I am new to integration testing and I am trying to run a suite of tests in a spring boot project using PostgreSQLContainers from the Testcontainers library.
The structure of the suite looks like this:
- Suite
- @SpringBootTest First Class
- @Test method
- @Test method
- etc.
- @SpringBootTest Second Class
- @Test method
- @Test method
- etc.
- @SpringBootTest First Class
I want to be able to spin up a new, empty container for each class, fill it with a .sql script, run some tests and tear down the container. The issue I am experiencing is that when I try to make the container for the second class the data from the first class is still in the container and I get sql errors because it's trying to re use primary keys.
I am able to run each class individually and the containers work as expected because they are isolated. As soon as I try to run them together in the suite the data seems like it is persisting between the containers.
The general structure of each class is like this:
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureTestDatabase
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class TestClass1 {
@Container
private static PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
@Test
@Sql("classpath:sql/script.sql")
void testMethod1() {
...
}
@Test
void testMethod2() {
...
}
}
Like I said, I am new to Testcontainers so I am wondering if I am using it wrong or if it is suited to doing these types of suites.