I have a bunch of spring boot integration tests and I am using testcontainers to manage dependencies like the DB. In order to reuse the test container, I am cleaning up the entire DB right before each test run. All that implementation is described here - How can I run @BeforeEach method BEFORE @Sql script for each test case in spring boot integration test?
Now I was able to run all my tests together just fine. Spring would load the test context, the DB migration (liquibase) would run and the entire DB structure would spin up. Now it doesn't matter if I was cleaning up all of my tables, including the DB migration log table (DBCHANGELOG in my case), all the tests were running fine as the context will not reload.
The problem arises when I introduced a @SpyBean. Apparently, wiring a dependency with @SpyBean caused the spring boot context to initialise again and run the DB migration in the same DB again. But unfortunately it fails everything as the tables are already there and it would try to create them from scratch.
I created a sample application to demonstrate this - https://github.com/jainishan/spring-boot-tryouts/blob/main/src/test/java/com/samples/sample/SpyBeanTest.java.
If you run all of the tests together, you will see that because of @SpyBean, this particular test was re running the DB migration (you can see that in the logs).
How can I make sure that when I run all my integration tests, the context is loaded once, the DB migration is ran once and then all the test cases reuse the same resources ?