我已经通过这里的问答环节对这个问题进行了一些研究。许多问题似乎都解决了这个问题,但实际上并非如此。
事情是这样的:
我得到一个
CaseController
, 委托(delegate)给CaseService
, 又委托(delegate)给CaseRepository
.我得到了
@Transactional
在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));
}
在这个测试中,我删除了一个在启动时为我插入的案例,感谢 hibernate 和 import.sql
测试成功,问题是我想在接下来的测试中继续解决这个案例,但是事务似乎没有回滚,并且案例被永久删除并且不能用于下一个测试。
我试过移动
@Transactional
来自CaseServiceImpl
到CaseController
,但这并没有什么不同。我可以说我的下杠杆
CaseRepositoryTest
每次测试后成功执行回滚。
在 CaseControllerIntegrationTest
之上我得到了:
@ActiveProfiles("integration-test")
@Transactional
@TransactionConfiguration
@IntegrationTest
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfiguration.class)
public class CaseControllerIntegrationTest {
...
AppConfiguration
看起来像这样:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableTransactionManagement
public class AppConfiguration {
}
我使用的数据库是hsqldb
最后一条重要信息 - 日志实际上表明回滚已完成:
2014-05-15 07:54:44.391 TRACE - o.s.t.c.t.TransactionalTestExecutionListener - Ending transaction for test context [DefaultTestContext@2eb0cefe testClass = CaseControllerIntegrationTest, testInstance = om.services.casemanagement.web.CaseControllerIntegrationTest@5a2ae1ab, testMethod = verifyDeleteSuccessfulOnExistingCase@CaseControllerIntegrationTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@2bec068b testClass = CaseControllerIntegrationTest, locations = '{}', classes = '{class om.services.AppConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{integration-test}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', parent = [null]]]; transaction status [org.springframework.transaction.support.DefaultTransactionStatus@3869a6e5]; rollback [true]
2014-05-15 07:54:44.403 INFO - o.s.t.c.t.TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [DefaultTestContext@2eb0cefe testClass = CaseControllerIntegrationTest, testInstance = om.services.casemanagement.web.CaseControllerIntegrationTest@5a2ae1ab, testMethod = verifyDeleteSuccessfulOnExistingCase@CaseControllerIntegrationTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@2bec068b testClass = CaseControllerIntegrationTest, locations = '{}', classes = '{class om.services.AppConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{integration-test}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', parent = [null]]]
我正在使用 Spring 4.0.3 发行版。
有什么想法吗?
最佳答案
看看你在这里做了什么:
您从 thread1 发送了一个 http 请求,该请求被 Web 容器的 thread2 捕获和处理。前者无法控制后者管理的交易。
如果您拥有一个CaseController
实例并直接从测试方法显式调用它的方法,您将享受@Transactional
提供的自动回滚功能>.
关于spring-mvc - Spring集成测试不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23676389/