Maven不运行黄瓜测试

时间:2020-08-27 23:35:16

标签: java spring maven junit cucumber

我有一个带有JUnit 5和Cucumber(v6.5.1)测试的Maven / Spring Boot 2.3.3应用程序。

问题是我可以通过Maven进行单元和集成测试,但不能运行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 {
}

黄瓜Spring Context配置:

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 {
    
    // steps here
}

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

但是,黄瓜测试执行没有迹象。

我什么都没做或做错了什么?

3 个答案:

答案 0 :(得分:1)

您正在使用将Cucumber与JUnit 4集成的cucumber-junit。您还在使用JUnit5。JUnit5可以通过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

答案 1 :(得分:1)

我遇到了同样的问题;当我运行 mvn test 命令时,没有执行黄瓜测试。我正在使用:

  • spring-boot:2.5.0
  • Java 11(OpenJDK Zulu 11.0.11)
  • 依赖项
    • 黄瓜核心:6.10.4
    • cucumber-java:6.10.4
    • cucumber-junit:6.10.4
    • 黄瓜弹簧:6:10.4

我所做的是从 junit-jupiter-engine 依赖项中排除 spring-boot-starter-test

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

答案 2 :(得分:0)

如果您使用junit 5,您要做的就是添加两个黄瓜依赖项(cucumber-java和cucmber-junit,将junit 4从所有依赖项中排除)。

此外,导入junit-vintage-engine,以便黄瓜可以使用它。

说明:

如果导入junit 4,将忽略所有使用木星(junit 5)的测试,反之亦然。您可以通过在每个junit版本中添加两个类(通过在每个类上添加每个版本的@Test)来对其进行测试。只有一个版本可以运行。