Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring集成测试不会回滚_Spring Mvc_Testing_Transactions_Integration Testing_Rollback - Fatal编程技术网

Spring mvc Spring集成测试不会回滚

Spring mvc Spring集成测试不会回滚,spring-mvc,testing,transactions,integration-testing,rollback,Spring Mvc,Testing,Transactions,Integration Testing,Rollback,关于这个问题,我在这里的问答中做了一些研究。许多问题似乎都能解决这个问题,但不是真的 所以,事情是这样的: 我得到了一个CaseController,它委托给CaseService,然后再委托给CaseRepository 我在CaseServiceImpl 我得到了一个CaseControllerIntegrationTest,从中我对CaseController执行RESTful请求并测试整个周期 问题是-我的事务没有回滚 其中一项测试是 @Test public void verif

关于这个问题,我在这里的问答中做了一些研究。许多问题似乎都能解决这个问题,但不是真的

所以,事情是这样的:

  • 我得到了一个
    CaseController
    ,它委托给
    CaseService
    ,然后再委托给
    CaseRepository

  • 我在
    CaseServiceImpl

  • 我得到了一个
    CaseControllerIntegrationTest
    ,从中我对
    CaseController
    执行RESTful请求并测试整个周期

问题是-我的事务没有回滚

其中一项测试是

@Test 
public void verifyDeleteSuccessfulOnExistingCase() {

        final String urlWithPlaceholders = serverPrefix + RequestMappings.CASES_RESOURCE_MAPPING + "/{caseId}";

        final ResponseEntity<CaseResource> response =
                restTemplate.exchange(
                        urlWithPlaceholders,
                        HttpMethod.DELETE,
                        null,
                        CaseResource.class, existingWsId_1, caseId);

        assertThat(response, notNullValue());
        assertThat(response.getBody(), nullValue());
        assertThat(response.getStatusCode(), is(HttpStatus.NO_CONTENT));
        assertThat(caseRepository.exists(caseId), is(false));

}
AppConfiguration
如下所示:

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableTransactionManagement
public class AppConfiguration {
}
我使用的数据库是hsqldb

最后一条重要信息-日志实际上表明回滚已完成:

2014-05-15 07:54:44.391痕迹- o、 s.t.c.t.TransactionalTestExecutionListener-结束的交易 测试上下文[DefaultTestContext@2eb0cefe测试类= 案例控制员整合测试= om.services.casemanagement.web。CaseControllerIntegrationTest@5a2ae1ab, testMethod=verifyDeleteSuccessfulOnExistingCase@CaseControllerIntegrationTest, testException=[null],mergedContextConfiguration= [WebMergedContextConfiguration@2bec068b测试类= CaseControllerIntegrationTest,位置={},类={class om.services.AppConfiguration}',contextInitializerClasses='[]', activeProfiles='{integration test}',resourceBasePath= “src/main/webapp”,上下文加载器= “org.springframework.boot.test.SpringApplicationContextLoader”,父级 =[null]]];事务状态[org.springframework.transaction.support]。DefaultTransactionStatus@3869a6e5]; 回滚[真]

2014-05-15 07:54:44.403信息- o、 s.t.c.t.TransactionalTestExecutionListener-回滚事务 测试上下文的测试执行后[DefaultTestContext@2eb0cefe testClass=CaseControllerIntegrationTest,testInstance= om.services.casemanagement.web。CaseControllerIntegrationTest@5a2ae1ab, testMethod=verifyDeleteSuccessfulOnExistingCase@CaseControllerIntegrationTest, testException=[null],mergedContextConfiguration= [WebMergedContextConfiguration@2bec068b测试类= CaseControllerIntegrationTest,位置={},类={class om.services.AppConfiguration}',contextInitializerClasses='[]', activeProfiles='{integration test}',resourceBasePath= “src/main/webapp”,上下文加载器= “org.springframework.boot.test.SpringApplicationContextLoader”,父级 =[null]]]

我正在使用Spring4.0.3发行版

有什么想法吗

看看你在这里做什么:

您从thread1发送http请求,该请求由web容器的thread2捕获并处理。前者无法控制后者管理的交易

如果您持有一个
CaseController
实例,并直接从测试方法显式调用其方法,您将享受
@Transactional

提供给您的自动回滚功能。请参见此处的操作:

您从thread1发送http请求,该请求由web容器的thread2捕获并处理。前者无法控制后者管理的交易

如果您持有一个
CaseController
实例,并直接从测试方法显式调用其方法,您将享受
@Transactional

提供给您的自动回滚功能,这种方式对我很有效

步骤1:创建一个私人案例服务,并在其上添加@Autowired。还添加一个私有MockMvc变量。 步骤2:创建一个setup方法并用@Before注释它。 步骤3:创建测试方法,用@test和@Transactional注释它。 这种方式适合我,我可以在没有任何回滚问题的情况下获得响应。

这种方式适合我

步骤1:创建一个私人案例服务,并在其上添加@Autowired。还添加一个私有MockMvc变量。 步骤2:创建一个setup方法并用@Before注释它。 步骤3:创建测试方法,用@test和@Transactional注释它。
这种方式对我来说很有效,我能够在没有任何回滚问题的情况下得到响应。

尽管@kumetix提到了它,但正确的答案是,没有办法回滚测试方法中通过RestTemplate发出的请求触发的事务。

您唯一的选择是通过手动删除回滚事务

请参考此以获取其他意见


希望这会有所帮助。

尽管@kumetix提到了它,但正确的答案是,无法回滚测试方法中通过RestTemplate发出的请求触发的事务。

您唯一的选择是通过手动删除回滚事务

请参考此以获取其他意见

希望有帮助

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableTransactionManagement
public class AppConfiguration {
}
private MockMvc restCaseController;

@Autowired
private CaseService caseService; 
@Before
private void setup() {
  MockitoAnnotations.initMocks(this);
  CaseController casecontroller = new CaseController(caseService);
  this.restCaseController = 
  MockMvcBuilders.standaloneSetup(casecontroller).build();
}
@Test
@Transactional
public void testMethod() {
    restCaseController.perform(post("/api/post")
                      .contentType(MediaType.APPLICATION_JSON_UTF8)
                      .content(jsonObject))
                      .andExpect(status().isCreated());
}