我有一个基本的Maven/Spring测试项目,其中包含一些简单的JUnit测试和Selenium/Cucumber测试,我以后想使用Github Action运行这些测试,但首先我想确保它们在本地运行.

这是JUnit的"测试":

package com.example.CucumberSelenium;

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

import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest
public class GithubActionsTests {

    @Test
    public void test() {
        assertTrue(true);
    }
}

这是我的Cucumber Test Runner文件:

package com.example.CucumberSelenium;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/com/example/CucumberSelenium/resources/features", glue = "com.example.CucumberSelenium.stepdefs")
public class CucumberTestRunnerTest {
}

这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>CucumberSelenium</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CucumberSelenium</name>
    <description>testing</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
            <version>7.11.2</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.11.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.17.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                        <include>**/*Test.java</include>
                        <include>**/GithubActionsTests.java</include>
                        <include>**/CucumberTestRunnerTest.java</include>
                    </includes>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

This is my file structure: enter image description here

这两个测试都可以使用IntelliJ自己运行得很好.但是当运行mvn test只运行黄瓜测试.

maven-surefire-plugin版本更改为2.22.2将反转结果,只有JUnit测试在运行,而不是黄瓜测试.所以我想有一些依赖性的问题或问题与插件,但我不知道什么.请告知🙏

推荐答案

您使用的是Spring Boot 3.2,它引入了JUnit 5.同时也使用了依赖于JUnit 4的cucumber-junit.

对JUnit 5的支持是在2.22版左右引入的.但是Surefire不能同时运行JUnit 4和JUnit 5,因此当两者都可用时,默认使用最新版本的JUnit.

在短期内,你可以通过包括junit-vintage-engine个,

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>

从长期来看,最好停止依赖JUnit 4,转而使用cucumber-junit-platform-engine.你可以在https://github.com/cucumber/cucumber-java-skeleton中找到一个工作示例(没有Spring).

Java相关问答推荐

具有额外列的Hibert多对多关系在添加关系时返回NonUniqueHealthExcellent

Android视图覆盖不阻止点击它后面的控件

gitlab ci不会运行我的脚本,因为它需要数据库连接'

在现代操作系统/硬件上按块访问数据值得吗?

neo4j java驱动程序是否会在错误发生时自动回滚事务?

Hibernate 6支持Joda DateTime吗?

在Spring终结点中,是否可以同时以大写和小写形式指定枚举常量?

S,要对Java复制构造函数深度克隆所有属性进行单元测试,最可靠的方法是什么?

Spring安全令牌刷新和JWT签名与本地计算的签名不匹配

垃圾收集时间长,会丢弃网络连接,但不会在Kubernetes中反弹Pod

Java 11 HttpCookie.parse在解析包含JSON的Cookie时引发IlLegalArgumentException

如何在列表(链表)中插入一个新 node (作为prelast)

使IntelliJ在导入时优先 Select 一个类或将另一个标记为错误

JavaFX复杂项目体系 struct

如何在Spring Security中设置一个任何人都可以打开的主页?

Java递归泛型是否可以被视为继承和重写的语法糖

如何在JSP中从select中获取值并将其放入另一个select

错误:JOIN/ON的参数必须是boolean类型,而不是bigint类型.Java Spring启动应用程序

如何使用java区分以下结果

关于正则表达式的一个特定问题,该问题与固定宽度负向后看有关