post 中的 REST ASURED 状态代码有问题

Problem with REST ASSURED status code in post

之前的整个代码都有效。 但是当我到达 post 时,它不想检查状态代码。它就像一个错误。并没有显示错误。我收到的消息是:

java: method statusCode in interface io.restassured.response.ResponseOptions<T> cannot be applied to given types;
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
statusCode()' in 'io.restassured.response.ResponseOptions' cannot be applied to '(int)'

密码是:

package RestAssured;

import static io.restassured.RestAssured.*;
import io.restassured.response.Response;
import netscape.javascript.JSObject;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.equalTo;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.*;

public class MoiPrimeri {

    @Test
    public void testGet1(){
    baseURI = "https://petstore.swagger.io";
    given().get("/v2/pet/findByStatus?status=available").then().
            statusCode(200);
    }

    @Test
    public void testGet2(){
        baseURI = "https://petstore.swagger.io";
        given().get("/v2/store/order/3").then().statusCode(200).body("petId", equalTo(2));
    }

    @Test
    public void testPost1() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("type", "IT");
        map.put("message", "Ivan");
        System.out.println(map);
        JSONObject request = new JSONObject(map);
        System.out.println(request.toJSONString());

        baseURI = "https://petstore.swagger.io";
        given().body(request.toJSONString()).when().post("/v2/pet/2/uploadImage").statusCode(200);

    }}

问题是您错过了 Post API 中的 then() 方法。

  • 如果没有 then()statusCode() 会从响应中获取状态代码,然后 return int.

  • then() - 方法链,它改变对象,statusCode(200)表示比较状态码。

given().body(request.toJSONString()).when().post("/v2/pet/2/uploadImage").then().statusCode(200);