java重启:如何为端点调用类型创建抽象层
这是我对GET endpoints的重启调用:
public static Response getCall(int expectedStatusCode){
return given()
.port(PORT)
.contentType(ContentType.JSON)
.when()
.log().all()
.get(getEndpoint)
.then()
.log().all()
.assertThat()
.statusCode(expectedStatusCode).extract().response();
}
以下是对POST端点的调用:
public static Response postEndpoint(Request request, int expectedStatusCode) {
return given()
.port(PORT)
.contentType(ContentType.JSON)
.body(request)
.when()
.log().all()
.post(postEndpointURI)
.then()
.log().all()
.assertThat()
.statusCode(expectedStatusCode).extract().response();
}
由于代码是可见的,所以代码是冗余的,唯一的区别是调用、POST和GET的类型
我如何使代码抽象化,并使调用不同
# 1 楼答案
希望它能对你的冗余问题有所帮助。您可能会从这里得到更多改进重构的想法