我是在测试中使用休息的测试中接收java.lang.assertionerror
这是我第一次使用Gson并放心,我真的很努力地说实话。 我需要验证内容类型是JSON,但是测试失败了,您将在下面看到的以下消息。 我不是在写所有我所拥有的进口,如果需要,请告诉我,我会提供它们。
这是我编写的代码:
package Gson;
public class TestBase {
public RequestSpecification httpRequest;
public Response response;
public JsonPath jsonPathEvaluator;
@BeforeMethod
public void before_method(){
RestAssured.baseURI = "https://reqres.in/";
httpRequest = RestAssured.given();
}
@Test
public void test1(){
Reqres reqres = new Reqres("Olivera","tester");
httpRequest.header("Content-Type", "application/json");
httpRequest.body(new Gson().toJson(reqres));
response = httpRequest.post("/api/users");
jsonPathEvaluator = response.jsonPath();
Assert.assertEquals(response.statusCode(), 201);
Assert.assertEquals(response.header("Content-Type"),("application/json"));
Assert.assertEquals(jsonPathEvaluator.get("name").toString(),"Olivera");
}
}
这就是我得到的响应:
java.lang.AssertionError:
Expected :application/json
Actual :application/json; charset=utf-8
如何解决此问题?
This is the first time for me using Gson and REST Assured, and I am really struggling to be honest.
I need to verify that the content type is JSON and it is, but the test fails with the following message that you will see below.
I am not writing all the imports that I have, if it is needed please tell me and I will provide them.
This is the code that I have written:
package Gson;
public class TestBase {
public RequestSpecification httpRequest;
public Response response;
public JsonPath jsonPathEvaluator;
@BeforeMethod
public void before_method(){
RestAssured.baseURI = "https://reqres.in/";
httpRequest = RestAssured.given();
}
@Test
public void test1(){
Reqres reqres = new Reqres("Olivera","tester");
httpRequest.header("Content-Type", "application/json");
httpRequest.body(new Gson().toJson(reqres));
response = httpRequest.post("/api/users");
jsonPathEvaluator = response.jsonPath();
Assert.assertEquals(response.statusCode(), 201);
Assert.assertEquals(response.header("Content-Type"),("application/json"));
Assert.assertEquals(jsonPathEvaluator.get("name").toString(),"Olivera");
}
}
And this is the response I get:
java.lang.AssertionError:
Expected :application/json
Actual :application/json; charset=utf-8
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题是由休息确定的,会自动添加
charset = utf-8
,可以返回响应中。您可以断言它们如下:
或在请求标头中禁用自动
charset = utf-8
:The problem is Rest-Assured automatically add
charset=utf-8
, that may be returned back in response.You can assert them as follow:
or to disable automatic
charset=utf-8
in request header: