Search code examples
spring-bootdocker-compose

Spring Docker Compose Support is not available at runtime when running tests in Gradle


Spring Docker Compose Support arrived in Spring Boot 3.1. I have set this up to start and tear down the Docker services when doing ./gradlew bootRun and this works fine. The docs then say one should use the custom dependency handling developmentOnly to avoid bundling it:

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-docker-compose")
}

When running @SpringBootTest tests they fail on creating beans, as the services are not spun up and this is expected, as the official docs say that the support is disabled by default when running tests:

By default, Spring Boot’s Docker Compose support is disabled when running tests. To enable it, set spring.docker.compose.skip.in-tests to false.

I have then done this, but still it failed. There was no indication of the Docker Compose Support being loaded at all, actually, which made me wonder if the instructions really were correct ... So, just for kicks, I changed developmentOnly to implementation and lo and behold: it worked!

So it seems that developmentOnly is not sufficient to make the dependency available at runtime when running tests, so either I am missing some config or the Spring docs and/or implementation for Gradle are wrong.

Is there a way to make this work in Gradle currently without inadvertently bundling the optional dependency?

Related, unanswered questions that seem to indicate this is not new:


Solution

  • Starting with Spring Boot 3.2, a Gradle configuration testAndDevelopmentOnly is available for situations like this one.

    If you can upgrade to Spring Boot 3.2, change your dependency declaration from

    dependencies {
       developmentOnly("org.springframework.boot:spring-boot-docker-compose")
    }
    

    to

    dependencies { 
       testAndDevelopmentOnly("org.springframework.boot:spring-boot-docker-compose")
    }
    

    I've created a Spring Boot issue to add this to the documentation.