Rollback transaction after @Test
Just add @Transactional
annotation on top of your test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"testContext.xml"})
@Transactional
public class StudentSystemTest {
By default Spring will start a new transaction surrounding your test method and @Before
/@After
callbacks, rolling back at the end. It works by default, it's enough to have some transaction manager in the context.
From: 10.3.5.4 Transaction management (bold mine):
In the TestContext framework, transactions are managed by the TransactionalTestExecutionListener. Note that
TransactionalTestExecutionListener
is configured by default, even if you do not explicitly declare@TestExecutionListeners
on your test class. To enable support for transactions, however, you must provide aPlatformTransactionManager
bean in the application context loaded by@ContextConfiguration
semantics. In addition, you must declare@Transactional
either at the class or method level for your tests.
Aside: attempt to amend Tomasz Nurkiewicz's answer was rejected:
This edit does not make the post even a little bit easier to read, easier to find, more accurate or more accessible. Changes are either completely superfluous or actively harm readability.
Correct and permanent link to the relevant section of documentation about integration testing.
To enable support for transactions, you must configure a
PlatformTransactionManager
bean in theApplicationContext
that is loaded via@ContextConfiguration
semantics.
@Configuration
@PropertySource("application.properties")
public class Persistence {
@Autowired
Environment env;
@Bean
DataSource dataSource() {
return new DriverManagerDataSource(
env.getProperty("datasource.url"),
env.getProperty("datasource.user"),
env.getProperty("datasource.password")
);
}
@Bean
PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
In addition, you must declare Spring’s
@Transactional
annotation either at the class or method level for your tests.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Persistence.class, SomeRepository.class})
@Transactional
public class SomeRepositoryTest { ... }
Annotating a test method with
@Transactional
causes the test to be run within a transaction that will, by default, be automatically rolled back after completion of the test. If a test class is annotated with@Transactional
, each test method within that class hierarchy will be run within a transaction.