Integration Testing Kotlin with Docker: Advanced Level
Integration testing is a key stage for verifying the interaction of application components. In the Kotlin and Docker ecosystem, this process reaches a new level thanks to containerization. In this article, we will cover advanced techniques for setting up and executing integration tests using Docker, Testcontainers, and Kotlin DSL.
1. Basics: Testcontainers with Kotlin DSL
Testcontainers is a library for Java/Kotlin that manages the lifecycle of Docker containers within tests. A convenient DSL is available for Kotlin, simplifying configuration.
// build.gradle.ktsdependencies { testImplementation("org.testcontainers:testcontainers:1.20.0") testImplementation("org.testcontainers:postgresql:1.20.0") testImplementation("org.jetbrains.kotlin:kotlin-test")}Example test with PostgreSQL:
import org.testcontainers.containers.PostgreSQLContainerimport org.testcontainers.junit.jupiter.Containerimport org.testcontainers.junit.jupiter.Testcontainersimport org.junit.jupiter.api.Test
@Testcontainersclass UserRepositoryTest {
companion object { @Container val postgres = PostgreSQLContainer<Nothing>("postgres:15-alpine") .withDatabaseName("testdb") .withUsername("test") .withPassword("test") }
@Test fun `should connect to database`() { val jdbcUrl = postgres.jdbcUrl // Use jdbcUrl in DataSource println("Connected to: $jdbcUrl") }}2. Parallel Container Execution
To speed up tests, containers can be run in parallel. However, it is important to consider port conflicts. Use dynamic ports and synchronization via GenericContainer.
import org.testcontainers.containers.GenericContainerimport org.testcontainers.containers.Networkimport org.testcontainers.containers.wait.strategy.Waitimport java.time.Duration
class ParallelContainerTest {
companion object { private val network = Network.newNetwork()
private val redis1 = GenericContainer<Nothing>("redis:7-alpine") .withNetwork(network) .withNetworkAliases("redis1") .withExposedPorts(6379) .waitingFor(Wait.forListeningPort())
private val redis2 = GenericContainer<Nothing>("redis:7-alpine") .withNetwork(network) .withNetworkAliases("redis2") .withExposedPorts(6379) .waitingFor(Wait.forListeningPort())
init { redis1.start() redis2.start() } }
@Test fun `parallel containers test`() { val port1 = redis1.getMappedPort(6379) val port2 = redis2.getMappedPort(6379) println("Redis1 port: $port1, Redis2 port: $port2") }}Use Network for isolation and waitingFor to wait for container readiness.
3. Docker Compose in Tests
For complex scenarios (database + message broker + cache), it is convenient to use DockerComposeContainer. It loads docker-compose.yml and manages all services.
import org.testcontainers.containers.DockerComposeContainerimport java.io.File
class FullStackTest {
companion object { private val compose = DockerComposeContainer<Nothing>( File("src/test/resources/docker-compose.yml") ).apply { withExposedService("postgres_1", 5432) withExposedService("redis_1", 6379) withLocalCompose(true) start() } }
@Test fun `full integration test`() { val pgPort = compose.getServicePort("postgres_1", 5432) val redisPort = compose.getServicePort