首页 > 解决方案 > java.lang.AssertionError: 预期 [201] 但发现 [201]

问题描述

我对使用 RestAssured 进行测试和使用这些方法非常陌生。

这是我的代码

package com.123.tests;
import com.jayway.restassured.response.Response;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.specification.RequestSpecification;

public class PersonPostTest {

    @Test
    public void RegistrationSuccessful()
    {       
        RestAssured.baseURI ="https://reqres.in/api";
        RequestSpecification request = RestAssured.given();



        JSONObject obj = new JSONObject();
        obj.put("name", "morpheus"); 
        obj.put("job", "leader");

        request.body(obj.toString());
        Response response = request.post("/users");

        int statusCode = response.getStatusCode();
        Assert.assertEquals(statusCode, "201");
        String successCode = response.jsonPath().get("SuccessCode");
        Assert.assertEquals( "Got the correct code", successCode, "Success");
    }

}

and everything seems to be good but I get this below error.

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: RegistrationSuccessful
java.lang.AssertionError: expected [201] but found [201]

我似乎不明白问题是什么。任何帮助,将不胜感激。谢谢

标签: restapipostrest-assured

解决方案


的返回类型getStatusCode()是整数。statusCode您正在检查Integer( 201) 与 Object 类型的相等性。这就是这里的问题。试试下面的代码片段。有用。

    Response response = request.post("/users");
    int statusCode = response.getStatusCode();
    Assert.assertEquals(statusCode, 201);

推荐阅读