rest - 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]
我似乎不明白问题是什么。任何帮助,将不胜感激。谢谢
解决方案
的返回类型getStatusCode()
是整数。statusCode
您正在检查Integer( 201
) 与 Object 类型的相等性。这就是这里的问题。试试下面的代码片段。有用。
Response response = request.post("/users");
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 201);
推荐阅读
- api - 如何在 Postman 中将数据发布到 API 时设置环境变量
- vue.js - [Vue 警告]:缺少必需的道具:“productInfo”
- json - Django 项目:在 AJAX 中,解析器错误 SyntaxError:JSON 输入意外结束
- typescript - 承诺和打字稿
- bash - Jupyter 运行 Bash 命令而不打印输出
- jhipster - 多个集群节点的 JHipster elasticsearch 配置
- mysql - 问题::无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)
- c - CALL指令如何解析成十六进制?
- algolia - 谷歌在 algolia 自动完成上的建议
- ms-access - Access 中的路由时间总和?