Maven 测试不使用 Spring Boot 2.2 和 JUnit 5 的 运行 Cucumber 场景

Maven Tests Does Not Run Cucumber Scenarios with Spring Boot 2.2 and JUnit 5

我正在 Spring Boot 2.2.6 上尝试 JUnit 5 和 Cucumber,我的应用程序需要 BDD 场景和单元测试。我已经创建了一个虚拟 ping 控制器对应的功能文件,这些文件都可以。

当我 运行 mvn clean test 时,没有调用 Cucumber 测试。仅调用 JUnit 测试。但是,当单击 CucumberTest.javaRun Test 按钮时,我可以从 Intellij GUI 运行 Cucumber 场景。

这是我的 类:

DummyApplicationTests.java:

package com.a.dummy;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@ActiveProfiles("test")
public class DummyApplicationTests {

    @Test
    public void contextLoads() {
    }

}

CucumberTest.java:

package com.a.dummy.bdd;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features")
public class CucumberTest {
}

CucumberSpringContextConfiguration.java:

package com.a.dummy.bdd;

import com.a.dummy.DummyApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@ContextConfiguration(classes = DummyApplication.class)
public abstract class CucumberSpringContextConfiguration {

}

PingTest.java:

package com.a.dummy.bdd.steps;

import com.a.dummy.bdd.CucumberSpringContextConfiguration;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class PingTest extends CucumberSpringContextConfiguration {

    @When("^the client calls /ping")
    public void the_client_issues_GET_ping() {
        ...
    }

    @Then("^the client receives status code of (\d+)$")
    public void the_client_receives_status_code_of(int statusCode) {
        ...
    }

    @And("^the client receives ping response")
    public void the_client_receives_ping_response_body() {
       ...
    }
}

我错过了什么?

JUnit 5 支持尚未集成,由于 RunWith 是 Junit 4 注释,我不得不包含 junit-vintage。

我在使用 Spring boot 2.4.0 时遇到了同样的问题,因为它只支持 JUnit 5,而且 Cucumber 似乎只与 JUnit 4 (RunWith) 集成。返回 Spring boot 2.3.6(向后兼容 JUnit 4)解决了这个问题。

https://docs.spring.io/spring-boot/docs/2.3.6.RELEASE/reference/html/spring-boot-features.html#boot-features-testing

26.1. Test Scope Dependencies The spring-boot-starter-test “Starter” (in the test scope) contains the following provided libraries:

JUnit 5 (including the vintage engine for backward compatibility with JUnit 4): The de-facto standard for unit testing Java applications.

如果您刚刚升级到 Spring 2.4.0 而 Maven 没有 运行 集成测试,那是因为:

Cucumber is based on JUnit 4. If you’re using JUnit 5, remember to include junit-vintage-engine dependency, as well. For more information, please refer to JUnit 5 documentation.
Cucumber documentation

Spring Boot 2.4.0 reference 开始,您可以按如下方式修复它:

If you have tests that use JUnit 4, JUnit 5’s vintage engine can be used to run them. To use the vintage engine, add a dependency on junit-vintage-engine, as shown in the following example:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2021 年的答案可能是添加

<dependency>
  <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit-platform-engine</artifactId>
  <version>${cucumber.version}</version>
</dependency>

到您的 pom.xml 文件,因为这允许 Cucumber 与 Junit 5 一起工作。另请参阅 https://github.com/cucumber/cucumber-jvm/blob/main/junit-platform-engine/README.md 了解更多想法。