Home Bootstrap project with Spring Boot, Kotest, Testcontainers & MongoDB
Post
Cancel

Bootstrap project with Spring Boot, Kotest, Testcontainers & MongoDB

I use Spring Initializr to generate project structure and dependencies when I create a new project. After that, I need to add some additional dependencies and configuration to make it work with Kotest and Testcontainers. I must also add some configuration to make it work with MongoDB.

I created a project template with all dependencies and configuration needed to start working with Spring Boot, Kotest, Testcontainers & MongoDB.

Project template

The project template is available on my GitHub.

What is essential in the project:

1
2
3
4
5
6
7
8
9
10
11
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.testcontainers:junit-jupiter")
    testImplementation("org.testcontainers:mongodb")
    testImplementation("io.kotest:kotest-runner-junit5-jvm:5.5.5")
    testImplementation("io.kotest:kotest-assertions-core-jvm:5.5.5")
    testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.2")
    testImplementation("io.kotest.extensions:kotest-extensions-testcontainers:1.3.4")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@SpringBootTest
@ActiveProfiles("integration")
@AutoConfigureMockMvc
@Testcontainers
abstract class IntegrationSpec(body: ShouldSpec.() -> Unit = {}) : ShouldSpec(body) {
    override fun extensions(): List<Extension> = listOf(SpringExtension)

    @Autowired
    lateinit var mockMvc: MockMvc

    @Autowired
    lateinit var mongoOperations: MongoOperations

    override suspend fun beforeEach(testCase: TestCase) {
        super.beforeEach(testCase)
        mongoOperations.collectionNames.forEach { mongoOperations.dropCollection(it) }
    }

    companion object {
        @Container
        @JvmField
        var container = MongoDBContainer(DockerImageName.parse("mongo:6"))

        init {
            container.start()
        }

        @DynamicPropertySource
        @JvmStatic
        fun mongoDbProperties(registry: DynamicPropertyRegistry) {
            registry.add("spring.data.mongodb.uri") { container.replicaSetUrl }
        }
    }
}

With such a configuration, you can start writing tests using MongoDB runnig in a Docker container and create the whole Spring context.

This post is licensed under CC BY 4.0 by the author.

Quick dump MongoDB to BigQuery using Dataflow

Creating a Simple Application (Backend + Frontend) on a Local Minikube K8S Cluster

Comments powered by Disqus.