[ad_1]
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<> ......
[ad_2]