英文:
Maven does not run Cucumber tests
问题
我有一个使用Maven / Spring Boot 2.3.3的应用程序,其中包含JUnit 5和Cucumber(v6.5.1)测试。
问题在于,我可以通过Maven正常运行单元测试和集成测试,但无法运行Cucumber测试。
Cucumber运行器:
package cucumber.salad.api.integration.cucumber;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "classpath:features",
plugin = { "pretty", "html:target/reports/cucumber", "json:target/cucumber.json", "usage:target/usage.jsonx", "junit:target/junit.xml" },
extraGlue = "cucumber.salad.api.integration.cucumber.steps"
)
public class CucumberTest {
}
Cucumber Spring上下文配置:
package cucumber.salad.api.integration.cucumber;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import cucumber.salad.App;
import io.cucumber.spring.CucumberContextConfiguration;
@ContextConfiguration
@CucumberContextConfiguration
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class CucumberSpringContextConfig {
@LocalServerPort
protected int port;
}
步骤:
package cucumber.salad.api.integration.cucumber.steps;
import static org.assertj.core.api.Assertions.assertThat;
import org.springframework.beans.factory.annotation.Autowired;
import cucumber.salad.api.integration.cucumber.CucumberSpringContextConfig;
import cucumber.salad.api.service.DummyService;
import io.cucumber.java.en.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SaladStepDefinitions extends CucumberSpringContextConfig {
// 步骤在这里
}
我在Maven的pom.xml中使用了Surefire和Failsafe插件:
https://github.com/danieldestro/cucumber-salad/blob/master/pom.xml
这是项目仓库链接:https://github.com/danieldestro/cucumber-salad
我运行的命令:
mvn clean test integration-test
但是Cucumber测试似乎没有任何执行迹象。
我是不是漏掉了什么或者做错了什么?
英文:
I have a Maven / Spring Boot 2.3.3 application with JUnit 5 and Cucumber (v6.5.1) tests.
The thing is that I can run OK either Unit and Integration tests via Maven, but it does not run Cucumber.
Cucumber Runner:
package cucumber.salad.api.integration.cucumber;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "classpath:features",
plugin = { "pretty", "html:target/reports/cucumber", "json:target/cucumber.json", "usage:target/usage.jsonx", "junit:target/junit.xml" },
extraGlue = "cucumber.salad.api.integration.cucumber.steps"
)
public class CucumberTest {
}
Cucumber Spring Context Config:
package cucumber.salad.api.integration.cucumber;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import cucumber.salad.App;
import io.cucumber.spring.CucumberContextConfiguration;
@ContextConfiguration
@CucumberContextConfiguration
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class CucumberSpringContextConfig {
@LocalServerPort
protected int port;
}
Steps:
package cucumber.salad.api.integration.cucumber.steps;
import static org.assertj.core.api.Assertions.assertThat;
import org.springframework.beans.factory.annotation.Autowired;
import cucumber.salad.api.integration.cucumber.CucumberSpringContextConfig;
import cucumber.salad.api.service.DummyService;
import io.cucumber.java.en.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SaladStepDefinitions extends CucumberSpringContextConfig {
// steps here
}
I use Surefire and Failsafe in the Maven pom.xml:
https://github.com/danieldestro/cucumber-salad/blob/master/pom.xml
Here is the project repository: https://github.com/danieldestro/cucumber-salad
Command I run:
mvn clean test integration-test
But there is not a sign from Cucumber test execution.
I am missing anything or doing something wrong?
答案1
得分: 7
你正在使用集成了JUnit 4的cucumber-junit
。同时,你也在使用JUnit 5。JUnit 5可以通过junit-vintage-engine
执行JUnit 4的测试。然而,你已经将这个引擎从你的类路径中排除了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
要么包含junit-vintage-engine
,要么使用cucumber-junit-platform-engine
代替cucumber-junit
。
英文:
You're using cucumber-junit
which integrates Cucumber with JUnit 4. You're also using JUnit 5. JUnit 5 can execute JUnit 4 tests through the junit-vintage-engine
. However you've excluded this engine from your classpath.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
Either include the the junit-vintage-engine
or use the cucumber-junit-platform-engine
instead of cucumber-junit
.
答案2
得分: 7
我遇到了相同的问题;当我运行 mvn test
命令时,Cucumber 测试未被执行。我正在使用:
- spring-boot:2.5.0
- Java 11 (OpenJDK Zulu 11.0.11)
- 依赖项
- cucumber-core: 6.10.4
- cucumber-java:6.10.4
- cucumber-junit:6.10.4
- cucumber-spring:6:10.4
我所做的是从 spring-boot-starter-test
依赖中排除 junit-jupiter-engine
。
<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>
英文:
I had the same problem; when I ran the mvn test
command the cucumber tests were not executed. I was using:
- spring-boot:2.5.0
- Java 11 (OpenJDK Zulu 11.0.11)
- Dependencies
- cucumber-core: 6.10.4
- cucumber-java:6.10.4
- cucumber-junit:6.10.4
- cucumber-spring:6:10.4
What I did was exclude the junit-jupiter-engine
from the spring-boot-starter-test
dependency.
<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>
答案3
得分: 0
如果您使用 JUnit 5,您所需做的就是添加两个 Cucumber 依赖项(cucumber-java 和 cucumber-junit),同时从所有依赖项中排除 JUnit 4。
此外,导入 junit-vintage-engine 以便 Cucumber 可以使用它。
解释:
如果您导入 JUnit 4,所有带有 Jupiter(JUnit 5)的测试将被忽略,反之亦然。您可以通过在每个版本的类上添加相应版本的 @Test(在每个类上分别添加 JUnit 4 和 JUnit 5 的 @Test)来测试。只会有一个版本会运行。
英文:
If you use junit 5, all you have to do is to add the two cucumber dependencies (cucumber-java and cucmber-junit by excluding junit 4 from all your dependencies).
furthermore, import junit-vintage-engine so cucumber can use it.
Explanation :
If you import junit 4, all the tests with jupiter (junit 5) will be ignored, Or vice versa. You can test it by adding two classes with each version of junit (by adding @Test of each version on each class). Only one version will run.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论