Search code examples
springspring-bootmavencucumberjunit5

Why my cucumber context configuration is not recognized?


I use Spring Boot 3 and I want to run tests with JUnit/Cucumber. I can't launch my app while executing the test.

I have this stacktrace :

io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.

For example:

@CucumberContextConfiguration @SpringBootTest(classes = TestConfig.class) public class CucumberSpringConfiguration { } Or:

@CucumberContextConfiguration @ContextConfiguration( ... )
public class CucumberSpringConfiguration { }

I wonder How I can configurate it to work well.

Here is my entrypoint class for tests :

package com.alten.shop;

import io.cucumber.java.en.Given;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@RunWith(Cucumber.class)
@CucumberOptions(
    monochrome = true,
    features = { "classpath:features/products.feature" },
    glue = { "com.alten.shop.steps" },
        plugin = {"pretty", "json:target/cucumber-report.json"}
)
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ShopApplicationTests {


}

The definitions steps are recognized, but the config is bad.

I don't have any annotations on StepDefinitions type, only @Given, @When and @Then annotations on methods to fit the features :

public class StepDefinitions {
}

And it is my pom.xml :

<properties>
    <java.version>17</java.version>
    <jjwt.version>0.11.5</jjwt.version>
    <swagger.version>2.6.0</swagger.version>
    <cucumber.version>7.14.1</cucumber.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-spring -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit-platform-engine -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.14</version>
        </dependency>

    </dependencies>

Can somebody help me with this ?


Solution

  • You've instructed Cucumber to look for glue in the steps package:

    glue = { "com.alten.shop.steps" },
    

    However the class annotated with @CucumberContextConfiguration is in the com.alten.shop package.

    So Cucumber won't find it.

    You can either remove the glue (cucumber will default to the package of the runner class) or move the @CucumberContextConfiguration and @SpringBootTest annotation to a separate class the steps package.