简体   繁体   English

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

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

I am trying JUnit 5 and Cucumber at Spring Boot 2.2.6 and will need both BDD scenarios and unit tests at my application.我正在 Spring Boot 2.2.6 上尝试 JUnit 5 和 Cucumber,并且在我的应用程序中需要 BDD 场景和单元测试。 I have created a dummy ping controller corresponding feature file which are OK.我已经创建了一个虚拟 ping 控制器对应的功能文件,这些文件都可以。

Cucumber tests are not called when I run mvn clean test .当我运行mvn clean test时不会调用黄瓜测试。 Only JUnit test is called.仅调用 JUnit 测试。 However, I can run Cucumber scenarios from Intellij GUI when click on the Run Test button for CucumberTest.java .但是,当单击CucumberTest.javaRun Test按钮时,我可以从 Intellij GUI 运行 Cucumber 场景。

Here are my classes:这是我的课程:

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() {
       ...
    }
}

What I am missing?我缺少什么?

If you just upgraded to Spring 2.4.0 and maven doesn't run you integration tests, it's because:如果您刚刚升级到 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. Cucumber 基于 JUnit 4。如果您使用的是 JUnit 5,请记住也要包含 junit-vintage-engine 依赖项。 For more information, please refer to JUnit 5 documentation.有关更多信息,请参阅 JUnit 5 文档。
Cucumber documentation黄瓜文档

From Spring Boot 2.4.0 reference you can fix it as follows:Spring Boot 2.4.0 参考你可以修复它如下:

If you have tests that use JUnit 4, JUnit 5's vintage engine can be used to run them.如果您有使用 JUnit 4 的测试,则可以使用 JUnit 5 的老式引擎来运行它们。 To use the vintage engine, add a dependency on junit-vintage-engine, as shown in the following example:要使用老式引擎,请添加对 junit-vintage-engine 的依赖,如以下示例所示:

<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>

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

A 2021 answer could be to add 2021 年的答案可能是添加

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

to your pom.xml file as this allows Cucumber to work with Junit 5. See also https://github.com/cucumber/cucumber-jvm/blob/main/junit-platform-engine/README.md for more ideas.到您的 pom.xml 文件,因为这允许 Cucumber 与 Junit 5 一起使用。另请参阅https://github.com/cucumber/cucumber-jvm/blob/main/junit-platform-engine/README.md了解更多想法。

I have had the same issue with Spring boot 2.4.0 because it only supports JUnit 5, and seems like Cucumber only integrates with JUnit 4 (RunWith).我在 Spring boot 2.4.0 上遇到了同样的问题,因为它只支持 JUnit 5,而且似乎 Cucumber 只与 JUnit 4 (RunWith) 集成。 Go back to Spring boot 2.3.6 (which has backwards compatibility with JUnit 4) solved the issue.回到 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 https://docs.spring.io/spring-boot/docs/2.3.6.RELEASE/reference/html/spring-boot-features.html#boot-features-testing

26.1. 26.1. Test Scope Dependencies The spring-boot-starter-test “Starter” (in the test scope) contains the following provided libraries:测试范围依赖 spring-boot-starter-test “Starter”(在测试范围内)包含以下提供的库:

JUnit 5 (including the vintage engine for backward compatibility with JUnit 4): The de-facto standard for unit testing Java applications. JUnit 5(包括与 JUnit 4 向后兼容的老式引擎):单元测试 Java 应用程序的事实标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM