maven Junit5套件未发现任何测试(带有 cucumber 设置的 Spring Boot )

ymdaylpp  于 12个月前  发布在  Maven
关注(0)|答案(1)|浏览(150)

this example之后,我试图在JUnit 5中运行cucumber测试,但@Suite不喜欢我:

org.junit.platform.suite.engine.NoTestsDiscoveredException: Suite [org.ono.sprint.demo.CucumberRunnerTest] did not discover any tests

字符串
我花了一天的时间来研究它,如果有人能给我给予正确的提示,我会非常感激。这似乎是一个JUnit级别的问题,因为它甚至不识别测试类。
测试设置有一个空的@Suite@SpringBootTest类,它似乎是由 cucumber 特定的注解配置的。

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


.来做cucumber step definitions类中描述的实际测试,它附带了一个功能文件。我不认为这是一个cucumber问题在这个阶段,所以我省略了功能文件):

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);
  }

}


另外,实际的Sping 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>。

b4lqfgs4

b4lqfgs41#

所以,按照M. P. Korstanjes的建议,我得到了它的工作.非常感谢,谢谢!我相信这是缺乏junit-platform-suite依赖和,最重要的是一个错误的文件夹结构之间的混合:这个东西期望找到src/test/java下的包文件夹来精确复制src/main/java下的内容。看起来@SelectClassPathResource注解不起作用,福尔斯回到默认行为。另外,gherkin特性文件显然需要再次放在src/test/resources下,从那里复制包文件夹结构。
所以我的sprint现在看起来是这样的:

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


依赖关系如下。注意依赖关系管理部分,它是解决一些cucumber deps所需要的。

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

相关问题