为什么我的黄瓜上下文配置无法识别?

问题描述 投票:0回答:1

我使用 Spring Boot 3,我想使用 JUnit/Cucumber 运行测试。 执行测试时我无法启动我的应用程序。

我有这个堆栈跟踪:

io.cucumber.core.backend.CucumberBackendException:请注释一个 具有一些上下文配置的粘合类。

例如:

@CucumberContextConfiguration @SpringBootTest(类= TestConfig.class) public class CucumberSpringConfiguration { } 或者:

@CucumberContextConfiguration @ContextConfiguration( ... )
公共类 CucumberSpringConfiguration { }

我想知道如何配置它才能正常工作。

这是我的测试入口点类:

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 {


}

定义步骤可以识别,但配置很糟糕。

我在 StepDefinitions 类型上没有任何注释,只有

@Given
@When
@Then
对适合功能的方法进行注释:

public class StepDefinitions {
}

这是我的 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>

有人可以帮我吗?

spring spring-boot maven cucumber junit5
1个回答
0
投票

您已指示 Cucumber 在

steps
包裹中寻找胶水:

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

但是用

@CucumberContextConfiguration
注解的类位于
com.alten.shop
包中。

所以 Cucumber 找不到它。

您可以移除胶水(黄瓜将默认为跑步者类的包)或移动

@CucumberContextConfiguration
@SpringBootTest
注释到
steps
包中的单独类。

© www.soinside.com 2019 - 2024. All rights reserved.