我正在尝试使用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)
有人能帮我解决这个问题吗
1条答案
按热度按时间qc6wkl3g1#
我可以用这个来做工作
类别
并将要素放置在
src/test/resources/features
下