crathour
crathour

Reputation: 60

How do you get the setup method in a test (with @Before) to not rollback, even when you have a test method (with @Test) set to rollback?

I'm doing some test data load inside the setup() method. That is, I want this test data to be setup once and be available for all tests within the class.

However, a transaction test method (annotated with @Test and @Rollback(true), causes the setup method to rollback as well..

Is there a way to make sure that only the test method rolls back and not the setup? Note :- Can't use @BeforeClass, as I need access to Autowired beans from spring context, which are not available in static context of setup(), if it's annotated as @BeforeClass.

Thanks, Shekhar


PS : A similar question was asked in stackoverflow but never answered :- @Rollback(false) not working on @Before using SpringJUnit4ClassRunner

Upvotes: 2

Views: 335

Answers (1)

Fritz Duchardt
Fritz Duchardt

Reputation: 11930

Use @TestExecutionListener instead of @BeforeClass. That way your test context will be loaded before execution. Furthermore, the setup code is externalised an can be reused for other tests.

More informations can be found here: What is the difference between @BeforeClass and Spring @TestExecutionListener beforeTestClass()

Upvotes: 1

Related Questions