Search code examples
spring-bootcucumberbdd

@Given or @When annotation of cucumber is throwing errors related spring boot configuration


I am writing cucumber BDD test cases .
All the dependencies of cucumber are included in pom.xml

<dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.trivago.rta</groupId>
        <artifactId>cluecumber-report-plugin</artifactId>
        <scope>test</scope>
        <version>2.3.3</version>
    </dependency>

In My step definition file , i have included SprintBootTest annotation

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AccessProfileStepDefinition {

    private final Logger log = LoggerFactory.getLogger(AccessProfileStepDefinition.class);
    // Location of input payload, which will be used to send request to api server.

    private static final String root_folder = "/testdata/bdd/json_for_accessprofile/";
    private final static String create_useraccessprofile_payload = root_folder + "create_access_profile_req.json";

    @Autowired
    private AccessProfileHttpClient httpClient;

    @Given("I am cbx system user")
    public void i_am_cbx_system_user() {
        // Write code here that turns the phrase above into concrete actions
        throw new io.cucumber.java.PendingException();
    }

I get error - when i am run my test

mvn -DAccessProfileFeatureBDDTest clean test


java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

If i comment @Given clause in my step definition file, then i do not get the error related to @SpringBootConfiguration

Other files . is below

package com.igtb.dcp.cbxaccessprofile.bdd;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features/accessprofile/", plugin = {
        "json:target/cucumber-report/cucumber.json", "com.igtb.dcp.cbxaccessprofile.bdd.TestInitialization" })
public class AccessProfileFeatureBDDTest {

}

accessprofile.feature has the feature and is contained in src/test/resources/features/accessprofile/ folder


Solution

  • If i comment @Given clause in my step definition file, then i do not get the error related to @SpringBootConfiguration

    If you don't have a step definition in the class annotated with the context configuration Cucumber will not detect any context configuration at all and fall back to a GenericApplicationContext.

    java.lang.IllegalStateException: Unable to find a
    @SpringBootConfiguration, you need to use @ContextConfiguration or
    @SpringBootTest(classes=...) with your test
    

    With this error Spring is telling you that your @SpringBootTest couldn't find any configuration to build the application context from.

    You either have to explicitly reference a class annotated with @SpringBootconfiguration, or make sure your @SpringBootApplication annotated class is in the same package, or add a @ContextConfiguration annotation to the AccessProfileStepDefinition.