I am using a singleton test-container for running multiple integration tests like this :
@SpringBootTest(webEnvironment = RANDOM_PORT)
public abstract class BaseIT {
static final PostgreSQLContainer<?> postgreSQLContainer;
static {
postgreSQLContainer =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:13"))
.withDatabaseName("test")
.withUsername("duke")
.withPassword("s3cret")
.withReuse(true);
postgreSQLContainer.start();
}
@DynamicPropertySource
static void datasourceConfig(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
}
}
And then extending from the test the base it
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class SecondApplicationIT extends BaseIT{
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private TodoRepository todoRepository;
@AfterEach
public void cleanup() {
this.todoRepository.deleteAll();
}
@Test
void contextLoads() {
this.todoRepository.saveAll(List.of(new Todo("Write blog post", LocalDateTime.now().plusDays(2)),
new Todo("Clean appartment", LocalDateTime.now().plusDays(4))));
ResponseEntity<ArrayNode> result = this.testRestTemplate.getForEntity("/todos", ArrayNode.class);
assertEquals(200, result.getStatusCodeValue());
assertTrue(result.getBody().isArray());
assertEquals(2, result.getBody().size());
}
}
but now the container is running even after the SecondApplicationIT
is finished, how can i stop the container after finishing all test classes
which is extending the BaseIt
P.S:
i tried with @AfterEach
and stopping the container there but it did not work