I've done some research through the q&a around here regarding the issue. many questions seem to address it, but not really.
So here's the thing:
I got a
CaseController
, which delegates toCaseService
, which delegate in turn toCaseRepository
.I got
@Transactional
on top of each method onCaseServiceImpl
I got a
CaseControllerIntegrationTest
, from which I perform RESTful requests to myCaseController
and test full cycles.
Problem is - my transactions are not rolled back.
One of the tests is
@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));
}
In this test I delete a case which I had inserted for me in startup thanks to hibernate and import.sql
Test is successful, problem is that I want to keep on addressing this case in the next tests, but the transaction seems not be rolled back, and the case is permanently deleted and not available for the next tests.
I have tried moving
@Transactional
fromCaseServiceImpl
to theCaseController
, yet it didn't make a difference.I can say that my lower lever
CaseRepositoryTest
successfully performs the rollback after each test.
On top of CaseControllerIntegrationTest
I got:
@ActiveProfiles("integration-test")
@Transactional
@TransactionConfiguration
@IntegrationTest
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfiguration.class)
public class CaseControllerIntegrationTest {
...
AppConfiguration
looks like this:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableTransactionManagement
public class AppConfiguration {
}
The db I'm using is hsqldb
Last important piece of info - The log actually indicates that the rollback was done:
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]]]
I'm using Spring 4.0.3 distribution.
Any ideas?