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
java spring junit transactions


source to share


No one has answered this question yet

See similar questions:

1
JUnit: setting transaction boundaries for test class

or similar:

1898
How do you assert that a specific exception is thrown in JUnit 4 tests?
429
Where does the @Transactional annotation belong?
109
Junit - run setup method once
73
Running an application before and after Suite in jUnit 4.x
50
setUp / tearDown (@ Before / @ After) why do we need them in JUnit?
five
Unit life cycle
2
@Before and @ After the exception
1
Spring / JUnit - Run anything before any tests
1
Run JUnit setup and disable exactly once for any test (s)
0
How can I run junit tearDown if an exception is enabled on startup?



All Articles
Loading...
X
Show
Funny
Dev
Pics