I'm using testcontainer in my oss software but i think there is a problem in my configurations or in the docker/testcontainer runtime...
I have some tests and when they are running separated, everything works fine but when I try to run all tests the last on fail due to a problem when the application try to connect with the container..
Debuggingthe problem I found that the container started in one port but the application is trying connection in other port, most of then are used in the last test classes run
All tests running:
One of the failed tests show me this log:
And the container started when the class UserControllerTest
started is using another port, like this:
docker on windows showing the container port
My test configuration is based in an abstract class (see bellow) and, like a said, if a run the class who is showing errors alone, everything works fine.
@Testcontainers
@ActiveProfiles("test")
@ExtendWith(SpringExtension::class)
@TestMethodOrder(value = OrderAnnotation::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
abstract class AbstractTest {
companion object {
@Container
private val redisContainer = GenericContainer<Nothing>("redis:6-alpine")
.apply {
withExposedPorts(6379)
withCreateContainerCmdModifier { cmd -> cmd.withName("wb-test-cache") }
}
@Container
private val postgresContainer = PostgreSQLContainer<Nothing>("postgres:13-alpine")
.apply {
withExposedPorts(5432)
withUsername("sa_webbudget")
withPassword("sa_webbudget")
withDatabaseName("webbudget")
withCreateContainerCmdModifier { cmd -> cmd.withName("wb-test-database") }
}
@JvmStatic
@DynamicPropertySource
fun dynamicPropertiesRegister(registry: DynamicPropertyRegistry) {
registry.add("spring.datasource.url", postgresContainer::getJdbcUrl)
registry.add("spring.redis.host", redisContainer::getHost)
registry.add("spring.redis.port", redisContainer::getFirstMappedPort)
}
}
}
Someone have seen something like this an know how to solve it?