I'm developing a backend application in SpringBoot + JPA. I have a bunch of unit tests and integration tests, and the latter use the database. I'd like to do setup before all the tests and a teardown afterward.
Setup: I do this in @Before
and I use boolean setUpIsDone
to make sure it is done once. Works fine.
Teardown - here I have a problem:
- I can't use
@AfterClass
because it's static. In JPA MyRepository class is just an interface annotated with @Autowired. So there is no instance of repository that I can use. - I can't use
@After
because it would try to clean the test data after every test. I can't use any boolean flag like in case of@Before
- how would I determine which test is the last one? - Spring rollback mechanism has some flaws because it doesn't write to the database physically so sometimes tests are passing but don't when rollback is disabled
For now there are a few solutions coming to my mind, but none is satysfiyng:
- Setup and cleanup along with every test (too time consuming)
- Pick
other test framework than JUnit (I've read that only JUnit forces
static
@BeforeClass
and@AfterClass
)
Any help?