Run JUnit @Before and @After their own transaction

The closest question I've found is here:

JUnit: setting transaction boundaries for test class

The accepted answer makes it possible for the entire test to run in a transaction, but what I'm trying to do is to avoid wrapping the test case in a transaction. Is there a way to do this?

Method annotation with @Transactional:

@Transactional
@Before
public void setUp(){
    //set up, before every test method
}

@Test
public void test(){
}

@Transactional
@After
public void tearDown(){
   //tear down after every test method
}

      

Doesn't work (I think this is to do with Spring proxies from what I've read).

This is due to a refactoring task in which we are currently using an abstract integration test base class that wraps each test in a transaction. I'm trying to reverse this, but all of our integration tests use DAO (not transactional) to set up and crash the database. I would like to be able to remake the DAO in transactional methods only in the setUp and tearDown methods (so I don't have to spend days refactoring many test classes).

If there are no better solutions?

thank

+3


source to share





All Articles