You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+JUnit5+Gradle环境下Cucumber测试未被检测到求助

解决Cucumber + Spring Boot + JUnit5集成测试找不到测试的问题

问题背景

使用Cucumber + Spring Boot + JUnit5编写集成测试,已配置Gradle依赖、Cucumber Runner类、测试步骤类,Feature文件放置在src/test/resources/com/abc/catalog路径下,但运行CucumberRunnerTests时提示:

No tests found for given includes: [com.abc.catalog.CucumberRunnerTests](--tests filter)

现有配置

Gradle依赖配置

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation("io.rest-assured:rest-assured:4.4.0")
    testImplementation("io.cucumber:cucumber-java:6.10.4")
    testImplementation("io.cucumber:cucumber-spring:6.10.4")
    testImplementation("io.cucumber:cucumber-junit-platform-engine:6.10.4")
    testRuntimeOnly("org.junit.platform:junit-platform-console")
    testImplementation group: 'org.junit.platform', name: 'junit-platform-suite-api', version: '1.9.0'
}

tasks.named('test') {
    useJUnitPlatform()
}

Cucumber Runner类

package com.abc.catalog;

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 static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/abc/catalog")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.abc.catalog")
public class CucumberRunnerTests {
}

测试步骤类

package com.abc.catalog;

import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsIterableContaining.hasItem;
import static org.hamcrest.core.StringContains.containsString;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.spring.CucumberContextConfiguration;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;

@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringbootCucumberTestDefinitions {

  private final static String BASE_URI = "http://localhost";

  @LocalServerPort
  private int port;

  private ValidatableResponse validatableResponse;

  private void configureRestAssured() {
    RestAssured.baseURI = BASE_URI;
    RestAssured.port = port;
  }

  protected RequestSpecification requestSpecification() {
    configureRestAssured();
    return given();
  }

  @Given("I send a request to the URL {string} to get user details")
  public void iSendARequest(String endpoint) throws Throwable {
    validatableResponse = requestSpecification().contentType(ContentType.JSON)
        .when().get(endpoint).then();
    System.out.println("RESPONSE :"+validatableResponse.extract().asString());
  }

  @Then("the response will return status {int} and id {int} and names {string} and passport_no {string}")
  public void extractResponse(int status, int id, String studentName,String passportNo) {
    validatableResponse.assertThat().statusCode(equalTo(status))
        .body("id",hasItem(id)).body(containsString(studentName))
        .body(containsString(passportNo));
  }
}

解决方案

1. 修复JUnit Platform Suite API版本兼容性问题

Cucumber 6.10.4与JUnit Platform Suite API 1.9.0版本不兼容,需将依赖版本降级到1.7.x系列(与Cucumber 6.x兼容的稳定版本):
修改Gradle依赖中的junit-platform-suite-api配置:

testImplementation group: 'org.junit.platform', name: 'junit-platform-suite-api', version: '1.7.2'

2. 验证Feature文件的正确性

  • 确保Feature文件以.feature为后缀(例如user-details.feature),文件名不能有误
  • 确认文件路径src/test/resources/com/abc/catalog与Runner类中@SelectClasspathResource("com/abc/catalog")的配置完全匹配
  • 检查Feature文件内容格式,确保符合Gherkin语法,例如:
    Feature: Get User Details
      Scenario: Retrieve user information
        Given I send a request to the URL "/users/1" to get user details
        Then the response will return status 200 and id 1 and names "John Doe" and passport_no "P1234567"
    

3. 调整测试运行方式

  • 不要直接在IDE中运行CucumberRunnerTests类,而是通过Gradle命令执行测试:
    ./gradlew test
    
  • 如果使用IDE(如IntelliJ IDEA),需确保已安装Cucumber插件,并且将测试运行器配置为JUnit Platform

4. 检查Glue路径配置

确认@ConfigurationParameter中的GLUE_PROPERTY_NAME值com.abc.catalog与测试步骤类所在包一致,确保没有拼写错误

内容的提问来源于stack exchange,提问作者Patan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 06:18:29