以下 这个例子,我正在尝试在 JUnit5 中运行黄瓜测试,但 @Suite 不喜欢我:
org.junit.platform.suite.engine.NoTestsDiscoveredException: Suite [org.ono.sprint.demo.CucumberRunnerTest] did not discover any tests
我现在已经花了一天的大部分时间来摆弄它并研究它,如果有人能给我正确方向的提示,我将非常感激。这似乎是 JUnit 级别的问题,因为它甚至无法识别测试类。
测试设置有一个空的
@Suite
, @SpringBootTest
类,它似乎是由黄瓜特定的注释配置的...
package org.ono.sprint.demo;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
import org.springframework.boot.test.context.SpringBootTest;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "org.ono.sprint.demo")
@CucumberContextConfiguration
@SpringBootTest
public class CucumberRunnerTest {
}
...进行黄瓜步骤定义类中描述的实际测试,该类附带一个功能文件。我认为现阶段这不是黄瓜问题,所以我省略了功能文件):
package org.ono.sprint.demo;
import org.junit.jupiter.api.Assertions;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.web.client.RestTemplate;
import io.cucumber.java.en.When;
public class StepDefinitionsTest {
@LocalServerPort
private int port;
private final RestTemplate restTemplate = new RestTemplate();
@When("I request a greeting")
public void i_request_a_greeting() {
String greetingUrl = "http://localhost:" + port + "/greetings";
String greeting = restTemplate.getForObject(greetingUrl, String.class);
Assertions.assertEquals("Hello", greeting);
}
}
另外,实际的 Spring Boot 实现是最简单的,所以省略。
这些是 Maven 依赖项:
<properties>
<java.version>17</java.version>
<junit-platform.version>1.9.1</junit-platform.version>
<cucumber.version>7.10.1</cucumber.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-test</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-console-standalone</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
我正在使用
<artifactId>spring-boot-maven-plugin</artifactId
>。
因此,按照 M.P.Korstanjes 的建议,我让它发挥作用。万分感谢,谢谢!我相信这是缺乏
junit-platform-suite
依赖关系和最重要的是错误的文件夹结构之间的混合:这个东西期望找到 src/test/java
下的包文件夹来准确复制 src/main/java
下的内容。看起来 @SelectClassPathResource
注释不起作用并回退到默认行为。此外,小黄瓜功能文件显然需要再次位于 src/test/resources
下,从那里复制包文件夹结构。事实并非如此。src/
main/
java/
org/
ono/
sprint/
demo/
DemoApplication.java
HomeController.java
test/
java/
org/
ono/
sprint/
demo/
SmokeTest.java
CucumberRunnerTest.java
resource/
org/
ono/
sprint/
demo/
helloworld.feature
和
CucumberRunnerTest
就像这样:
package org.ono.sprint.demo;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
import org.springframework.boot.test.context.SpringBootTest;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "org.ono.sprint.demo")
@CucumberContextConfiguration
@SpringBootTest
public class CucumberRunnerTest {
}
依赖关系如下。请注意解决某些黄瓜依赖项所需的依赖项管理部分。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.14.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>