spring-bootcucumberjunit5testcontainers-junit5cucumber-spring

Junit 5's @Testcontainers doesn't work with cucumber and spring boot 3


I wrote some cucumber test and noticed that @Testcontainers don't work when used with cucumber. If I run my cucumber tests like this:

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.OracleContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@Testcontainers
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@ActiveProfiles("h2")
public class CucumberSpringConfiguration {
    @Container
    @ServiceConnection
    static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart");

    @Container
    @ServiceConnection
    static KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"));
}

The spring application will fail with:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Mapped port can only be obtained after the container is started
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:177)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
    ... 76 more
Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
    at org.testcontainers.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:512)
    at org.testcontainers.containers.ContainerState.getMappedPort(ContainerState.java:161)
    at org.testcontainers.containers.OracleContainer.getOraclePort(OracleContainer.java:194)
    at org.testcontainers.containers.OracleContainer.getJdbcUrl(OracleContainer.java:120)
    at org.springframework.boot.testcontainers.service.connection.jdbc.JdbcContainerConnectionDetailsFactory$JdbcContainerConnectionDetails.getJdbcUrl(JdbcContainerConnectionDetailsFactory.java:65)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:56)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:117)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140)
    ... 77 more

This is because the containers were not started so @Testcontainers doesn't seem to do anything. I also noticed that if I put in this class @BeforeAll/ @AfterAll from junit package, they are not being invoked at all so junit integration seems pretty dead to me.

Now, if I manually start the containers in cucumber's @BeforeAll/ @AfterAll, my containers will be started and everything will work:

import io.cucumber.java.AfterAll;
import io.cucumber.java.BeforeAll;
import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.OracleContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@Testcontainers
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@ActiveProfiles("h2")
public class CucumberSpringConfiguration {
    @Container
    @ServiceConnection
    static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart");

    @Container
    @ServiceConnection
    static KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"));

    @BeforeAll
    public static void setUp() {
        System.out.println("++++++++ BEFORE ALL ++++++++");

        oracleContainer
                .withReuse(true)
                .start();

        kafkaContainer
                .withReuse(true)
                .start();
    }

    @AfterAll
    public static void tearDown() {
        System.out.println("++++++++ AFTER ALL ++++++++");

        oracleContainer.stop();
        kafkaContainer.stop();
    }
}

Notice that I am using BeforeAll/ AfterAll from cucumber package and not junit.

In all the tutorials that I have seen on the internet, @Testcontainer seems to work fine with cucumber.

I am using spring boot 3.3.0 and the following junit/ cucumber deps:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-bom</artifactId>
            <version>7.18.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.10.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit-platform-engine</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-testcontainers</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>oracle-xe</artifactId>
        <version>1.19.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>kafka</artifactId>
        <version>1.19.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>1.19.8</version>
        <scope>test</scope>
    </dependency>

The cucumber config class looks like:

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;


@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.cucumber")
public class CucumberRunnerTest {
}

What could be the problem?


Solution

  • You're spot on! The container is not starting because @TestContainers and @Container annotations both belong to junit-jupiter-engine. Cucumber uses junit-platform-suite-engine (here is the list of JUnit 5 test engines).

    I managed to make it work by removing both annotations that are useless and starting the container programmatically, just like you did.

    However, you still can count on the Spring Boot 3 @ServiceConnection annotation to automatically set up the connection pool for you.

    N.B. for readers:

    1. @BeforeAll and @AfterAll come from io.cucumber.java (as already mentioned)
    2. I don't have the issue of .withReuse(true) allowing multiple applications use the same container because I run the tests in a pipeline where each step runs in its own container.

    Here's how my configuration classes look like:

    import io.cucumber.java.BeforeAll;
    import io.cucumber.spring.CucumberContextConfiguration;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
    import org.testcontainers.containers.PostgreSQLContainer;
    
    @CucumberContextConfiguration
    @SpringBootTest
    public class CucumberSpringConfiguration {
    
        @ServiceConnection
        static final PostgreSQLContainer<?> postgreSQLContainer =
                new PostgreSQLContainer<>("postgres:16.2");
    
        @BeforeAll
        public static void setup() {
            postgreSQLContainer.withReuse(true).start();
        }
    
        @AfterAll
        public static void tearDown() {
            postgreSQLContainer.stop();
        }
    }
    
    

    and

    import static io.cucumber.core.options.Constants.FILTER_TAGS_PROPERTY_NAME;
    import static io.cucumber.core.options.Constants.PLUGIN_PROPERTY_NAME;
    
    import org.junit.platform.suite.api.ConfigurationParameter;
    import org.junit.platform.suite.api.SelectClasspathResource;
    import org.junit.platform.suite.api.SelectPackages;
    import org.junit.platform.suite.api.Suite;
    
    @Suite
    @SelectClasspathResource("features")
    @SelectPackages({"package-with-step-definitions"})
    @ConfigurationParameter(
            key = PLUGIN_PROPERTY_NAME,
            value =
                    "pretty,\n"
                            + "html:target/AcceptanceTestReports/report.html,\n" //
                            + "json:target/AcceptanceTestReports/report.json,\n" //
                            + "junit:target/AcceptanceTestReports/report.xml")