Search code examples
spring-bootdockerkotlintestcontainerstestcontainers-junit5

Testcontainers loss of connection after some tests running


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:

tests failing

One of the failed tests show me this log:

log of failed test

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?


Solution

  • After some research I figured out what is the problem: the context.

    When spring runs the first mvc controller test, it starts a single instance of tomcat to all controllers, this means when testcontainers recreate the docker instance for the database (after a new controller start testing) the properties (port, URL..) were not updated because spring will reuse the current instance of tomcat (from the last mvc test)

    Solution: mark the context as dirty for each test class, this will make spring recreate the context everytime a new test class starts and this will trigger the dynamicPropertiesRegister to update the properties correctly.

    I just had to add this annotation @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) to my AbstractTest