未发现 cucumber 测试 cucumber +春 Boot + Junit 5 + Gradle

mec1mxoz  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(187)

我正在尝试使用cubber + Sping Boot + Junit5编写集成测试
我有这些依赖项,我的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")
    // https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-api
    testImplementation group: 'org.junit.platform', name: 'junit-platform-suite-api', version: '1.9.0'
}

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

我有两节课
流道等级

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

  }

}

这些特性存在于src/test/resuorces/com/abc/catalog
运行CucumberRunnerTests测试时,未检测到测试

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

有人能帮我解决这个问题吗

qc6wkl3g

qc6wkl3g1#

我可以用这个来做工作

plugins {
    id 'org.springframework.boot' version "${gradleSpringBootPluginVersion}"
}

dependencies {
    implementation project(':catalog-common')
    implementation project(':catalog-model')
    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'
    implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: "${openApiDocsVersion}"

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

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

类别

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameters({
    @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME,
        value = "json:build/cucumber/cucumber.json"),
    @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.abc.catalog.bdd"),
    @ConfigurationParameter(key = FILTER_TAGS_PROPERTY_NAME, value = "not @Disabled"),
    @ConfigurationParameter(key = PLUGIN_PUBLISH_QUIET_PROPERTY_NAME, value = "true"),
    @ConfigurationParameter(key = JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long")
})
public class CatalogBddFeatures {

}

@CucumberContextConfiguration
@SpringBootTest(classes = CatalogApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
@TestPropertySource("classpath:test-application.properties")
public class SpringGlue {

}

并将要素放置在src/test/resources/features

相关问题