@Given 或 @When cucumber 注释抛出与 spring 引导配置相关的错误

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

我正在编写 Cucumber BDD 测试用例。
cucumber的所有依赖都包含在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>

在我的步骤定义文件中,我包含了 SprintBootTest 注释

@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();
    }

我收到错误 - 当我 运行 我的测试

mvn -DAccessProfileFeatureBDDTest clean test


java.lang.IllegalStateException:无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(类=...)

如果我在我的步骤定义文件中注释@Given 子句,那么我不会收到与@SpringBootConfiguration 相关的错误

其他文件。低于

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 具有此功能并包含在 src/test/resources/features/accessprofile/ 文件夹中

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

如果您在 class 中没有使用上下文配置注释的步骤定义,Cucumber 将根本不会检测任何上下文配置并回退到 GenericApplicationContext

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

这个错误 Spring 告诉你你的 @SpringBootTest 找不到任何配置来构建应用程序上下文。

您要么必须明确引用带有 @SpringBootconfiguration 注释的 class,要么确保您的 @SpringBootApplication 注释 class 在同一个包中,或者添加一个 @ContextConfiguration 注释到 AccessProfileStepDefinition