Home > Back-end >  How to configure testcontainers to run docker with gradle?
How to configure testcontainers to run docker with gradle?

Time:11-22

For my project I need to have a task that starts a test container, but when I am trying to start my task I have the following error

Previous attempts to find a Docker environment failed. Will not retry. Please see logs and check configuration

I think it's a configuration issue.

I am using kotlin gradle, my build file looks like this :

import org.testcontainers.containers.PostgreSQLContainer

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    testImplementation("org.testcontainers:postgresql")
}

buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        "classpath"("org.testcontainers:postgresql:1.17.5")
    }
}

tasks.register("postgresContainer") {
    val containerInstance = PostgreSQLContainer<Nothing>("postgres:12.8")
        .apply {
            withDatabaseName("test")
        }
    containerInstance.start()
    extra["containerUrl"] = containerInstance.jdbcUrl
    extra["containerUsername"] = containerInstance.username
    extra["containerPassword"] = containerInstance.password
    extra["containerDatabaseName"] = containerInstance.databaseName
    extra["containerInstance"] = containerInstance
}

I have omited most of it but cannot find what I am missing

I tried to refer to the documentation of test containers but cannot find my specific case, docker seems to be correctly configured, I think the error comes from my build.kts

CodePudding user response:

I would suggest the following.

Add this to your build.gradle.kts file to add support for test containers (this specific example is for PostgreSQL but the idea is the same...):

dependencies {
    testImplementation("org.testcontainers:junit-jupiter")
    testImplementation("org.testcontainers:postgresql:$testContainerVersion")
}

dependencyManagement {
    imports {
        mavenBom("org.testcontainers:testcontainers-bom:$testContainerVersion")
    }
}

Add the following to your tests class as a global variable:

val postgreSQLContainer = PostgreSQLContainer<Nothing>("postgres:14")
    .apply {
        withReuse(true)
        start()
    }

Personally I've added that into an AbstractIntegrationTest class and also configured the container there as follow:

abstract class AbstractIntegrationTest {
   companion object {
        @JvmStatic
        @DynamicPropertySource
        fun properties(registry: DynamicPropertyRegistry) {
            registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl)
            registry.add("spring.datasource.username", postgreSQLContainer::getUsername)
            registry.add("spring.datasource.password", postgreSQLContainer::getPassword)
        }
    }
}

Note that the withReuse(true) is just an optimization that ensures that the DB is not re-created for each test as this is a heavy operation, and therefore you need to programmatically truncate your tables between tests...

For instance:

@BeforeEach
fun setUp() {
    // Test util class that truncate all tables
    repoUtils.cleanTables()
}
  • Related