gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:19:41 26 4
gpt4 key购买 nike

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

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

这是我的类(class):
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() {
...
}
}

我缺少什么?

最佳答案

如果您刚刚升级到 Spring 2.4.0 并且 maven 没有运行您的集成测试,那是因为:

Cucumber is based on JUnit 4. If you’re using JUnit 5, remember toinclude junit-vintage-engine dependency, as well. For moreinformation, 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 beused to run them. To use the vintage engine, add a dependency onjunit-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>

关于java - Maven 测试不使用 Spring Boot 2.2 和 JUnit 5 运行 Cucumber 场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60982006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com