Integration Testing Kotlin with Docker: Advanced Level

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Integration Testing Kotlin with Docker: Advanced Level

\n\n

Integration testing is a critically important stage in the development of modern microservices in Kotlin. Using Docker allows you to create isolated, reproducible environments that are as close to production as possible. In this article, we will look at advanced techniques for integration testing Kotlin applications with Docker, including Testcontainers, performance optimization, and CI/CD integration.

\n\n

1. Setting up Testcontainers for a Kotlin Project

\n\n

Testcontainers is a library for JUnit that manages the lifecycle of Docker containers directly from tests. For Kotlin, it provides convenient DSL extensions.

\n\n

1.1. Adding Dependencies

\n\n

Add the following dependencies to build.gradle.kts:

\n\n
testImplementation("org.testcontainers:testcontainers:1.19.3")\ntestImplementation("org.testcontainers:postgresql:1.19.3")\ntestImplementation("org.testcontainers:junit-jupiter:1.19.3")\ntestImplementation("org.jetbrains.kotlin:kotlin-test")
\n\n

1.2. Basic Example with PostgreSQL

\n\n

Consider a test for a repository working with PostgreSQL:

\n\n
import org.junit.jupiter.api.Test\nimport org.testcontainers.containers.PostgreSQLContainer\nimport org.testcontainers.junit.jupiter.Container\nimport org.testcontainers.junit.jupiter.Testcontainers\n\n@Testcontainers\nclass UserRepositoryTest {\n\n    companion object {\n        @Container\n        val postgres = PostgreSQLContainer<Nothing>("postgres:15-alpine").apply {\n            withDatabaseName("testdb")\n            withUsername("test")\n            withPassword("test")\n        }\n    }\n\n    @Test\n    fun `should save and retrieve user`() {\n        val dataSource = createDataSource(postgres.jdbcUrl, postgres.username, postgres.password)\n        val repository = UserRepository(dataSource)\n\n        val user = User(id = null, name = "Alice", email = "alice@example.com")\n        val saved = repository.save(user)\n\n        val found = repository.findById(saved.id!!)\n        assert(found?.name == "Alice")\n    }\n\n    private fun createDataSource(jdbcUrl: String, username: String, password: String): DataSource {\n        // HikariCP or another connection pool\n        return HikariDataSource(HikariConfig().apply {\n            this.jdbcUrl = jdbcUrl\n            this.username = username\n            this.password = password\n            driverClassName = "org.postgresql.Driver"\n        })\n    }\n}
\n\n

Testcontainers automatically starts the container before tests and stops it after completion. This guarantees a clean test environment.

\n\n

2. Testing Multiple Services with Docker Compose

\n\n

In real microservices, interaction with several external systems is often required: databases, message queues, caches. Use DockerComposeContainer to spin up the full stack.

\n\n

2.1. Docker Compose Configuration

\n\n

Create a docker-compose-test.yml file:

\n\n
version: '3.8'\nservices:\n  postgres:\n    image: postgres:15-alpine\n    environment:\n      POSTGRES_DB: testdb\n      POSTGRES_USER: test\n      POSTGRES_PASSWORD: test\n  redis:\n    image: redis:7-alpine\n  rabbitmq:\n    image: rabbitmq:3-management-alpine
\n\n

2.2. Integration Test with Docker Compose

\n\n

An example of a test verifying service interaction with a database, cache, and queue:

\n\n
import org.junit.jupiter.api.Test\nimport org.testcontainers.containers.DockerComposeContainer\nimport org.testcontainers.junit.jupiter.Container\nimport org.testcontainers.junit.jupiter.Testcontainers\nimport java.io.File\n\n@Testcontainers\nclass OrderServiceIntegrationTest {\n\n    companion object {\n        @Container\n        val environment = DockerComposeContainer(\n            File("src/test/resources/docker-com                

Blogs

Book Recommendations