java.lang.AssertionError:预期为[201],但发现为[201]

时间:2018-07-12 12:58:15

标签: rest api post rest-assured

我对使用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]

我似乎不明白问题出在哪里。任何帮助,将不胜感激。谢谢

2 个答案:

答案 0 :(得分:1)

getStatusCode()的返回类型为Integer。 您正在检查statusCode与对象类型是否为整数(201)。这就是这里的问题。 尝试下面的代码段。可以。

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

答案 1 :(得分:0)

在此处检查对象类型:

Assert.assertEquals(statusCode, "201");

一个是整数,另一个是字符串。那就是失败的原因。确保将它们转换为相同类型。 将断言替换为以下内容:

Assert.assertEquals(statusCode, new Integer(201));