Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollback transaction after @Test

First of all, I've found a lot of threads on StackOverflow about this, but none of them really helped me, so sorry to ask possibly duplicate question.

I'm running JUnit tests using spring-test, my code looks like this

@RunWith(SpringJUnit4ClassRunner.class)   @ContextConfiguration(locations = {}) public class StudentSystemTest {      @Autowired     private StudentSystem studentSystem;      @Before     public void initTest() {     // set up the database, create basic structure for testing     }      @Test     public void test1() {     }         ...   } 

My problem is that I want my tests to NOT influence other tests. So I'd like to create something like rollback for each test. I've searched a lot for this, but I've found nothing so far. I'm using Hibernate and MySql for this

like image 713
Jan Vorcak Avatar asked Sep 27 '12 17:09

Jan Vorcak


People also ask

Does @transactional rollback?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What is @test annotation in spring boot?

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.

What is the use of @test in JUnit?

A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method executes the code under test.


1 Answers

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 a PlatformTransactionManager 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.

like image 184
Tomasz Nurkiewicz Avatar answered Sep 23 '22 20:09

Tomasz Nurkiewicz