java - 安心 : How to make the abstract layer for the type of endpoint call

标签 java rest-assured

这是我对 GET 端点的restAssure 调用:

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。

如何使代码抽象并让调用不同?

最佳答案

希望它对您的冗余问题有所帮助。您可能会从这里获得更多改进重构的想法。

private static final int PORT = 1922;
private static String getEndpoint;
private static String postEndpointURI;

/**
 * First part of the building up the request
 */
private static RequestSpecification buildRequest() {
    return given()
            .port(PORT)
            .contentType(ContentType.JSON)
            .when()
            .log().all();
}

/**
 * Relevant part of the GET request
 */
private static Response buildGetRequest() {
    return buildRequest().get(getEndpoint);
}

/**
 * Relevant part of the POST request
 */
private static Response buildPostRequest(Request request) {
    return buildRequest().body(request).post(postEndpointURI);
}

/**
 * Last bit redundant that was called by both calls
 */
private static Response extractResponse(Response response, int expectedStatusCode) {
    return response.then()
            .log().all()
            .assertThat()
            .statusCode(expectedStatusCode).extract().response();
}


/**
 * Your main GET method
 */
public static Response assertThatGetResponseStatusIs(int expectedStatusCode) {
    return extractResponse(buildGetRequest(), expectedStatusCode);
}

/**
 * Your main POST method
 */
public static Response assertThatPostResponseStatusIs(Request request, int expectedStatusCode) {
    return extractResponse(buildPostRequest(request), expectedStatusCode);
}

public static void main(String[] args) {
    System.out.println(assertThatGetResponseStatusIs(500));
    System.out.println(assertThatPostResponseStatusIs(request, 400));
}

关于java - 安心 : How to make the abstract layer for the type of endpoint call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47293127/

相关文章:

java - 为什么会抛出空指针异常?

java - RestAssured - 将列表作为 QueryParam 传递

java - 使用 Rest Assured 从 JSON 响应中获取所有 id

ssl - 放心的 SSL 连接

java - 请放心 : Extracting value from JSON Array

java - 正则表达式查找具有空值的属性

java - 什么是NullPointerException,我该如何解决?

java - 为什么 Java 需要接口(interface)而 Smalltalk 不需要?

java - 如何在scala akka中找到父 Actor

java - 从文件夹及其子文件夹中查找特定文件类型