У меня есть базовый тестовый проект Maven/Spring с несколькими простыми тестами JUnit и тестами Selenium/Cucumber, которые я позже хочу запустить с помощью действий Github, но сначала я хочу убедиться, что они выполняются локально.
Это «тест» 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:
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>
Это моя файловая структура:
Оба теста сами проходят нормально с использованием IntelliJ. Но при запуске mvn test запускаются только тесты огурца.
Изменение версии maven-surefire-plugin на 2.22.2 изменит результат: выполняется только тест JUnit, а не тесты огурца. Итак, я предполагаю, что есть какая-то проблема с зависимостью или с плагином, но я не могу понять, в чем именно. Посоветуйте пожалуйста 🙏




Вы используете Spring Boot 3.2, который поддерживает JUnit 5. Также вы используете cucumber-junit, который зависит от JUnit 4.
Поддержка JUnit 5 была введена в Surefire где-то в версии 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 и переключиться на огуречный-юнит-платформенный движок . Вы можете найти рабочий пример (без Spring) в https://github.com/cucumber/cucumber-java-skeleton.
Добавление
junit-vintage-engineсделало свое дело! Спасибо 🙏 Тоже учтуcucumber-junit-platform-engine.