ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(initializers = AbstractIT.DockerPostgreDataSourceInitializer.class)
@Testcontainers
public abstract class AbstractIT {
@SuppressWarnings("resource")
@Container
static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:14")
.withUsername("postgres")
.withPassword("postgres")
.withInitScript("sql/init.sql")
.withDatabaseName("test")
.withReuse(true);
static {
postgreDBContainer.start();
}
public static class DockerPostgreDataSourceInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext,
"spring.datasource.url=" + postgreDBContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreDBContainer.getUsername(),
"spring.datasource.password=" + postgreDBContainer.getPassword()
);
}
}
}
SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30000ms. o.h.engine.jdbc.spi.SqlExceptionHelper : Connection to localhost:49168 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
I created one instance of Textcontainers, I have a lot of integration tests, but only one test class is working, the rest cannot. because the connection is being closed.
Installing @DirtiesContext and changing the open connection interval does not solve the problem.
Decision.
delete an annotation @Container.
static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<> ......
However, this solution will not allow you to work with the database. Because when one test class will work. Then there will be an error that the table.... exists. That is, the test container will try to initiate the table creation script again. How can I make it so that I can reuse the test container (not create a new one) and at the same time, I can use one database initialization script for all test nodes?