2

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

Community
  • 1
  • 1
crathour
  • 60
  • 6

1 Answers1

1

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()

Community
  • 1
  • 1
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
  • 1
    Can I access autowired beans within the beforeTestClass override, in the class that I extend from AbstractTestExecutionListener? Doesn't seem to work. All the autowired beans appear to be null..Hence, I can't use them to load the test data. – crathour Sep 08 '15 at 13:42
  • 1
    Guess, can't use @Autowired annotation.Would have to use - testContext.getApplicationContext().getBean("") – crathour Sep 09 '15 at 09:01