我是在测试中使用休息的测试中接收java.lang.assertionerror

发布于 2025-01-25 11:39:10 字数 1231 浏览 2 评论 0原文

这是我第一次使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2025-02-01 11:39:10

该问题是由休息确定的,会自动添加charset = utf-8,可以返回响应中。

您可以断言它们如下:

Assert.assertEquals(response.header("Content-Type"),("application/json; charset=utf-8"));

或在请求标头中禁用自动charset = utf-8

RestAssured.config = RestAssured.config(config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false));

The problem is Rest-Assured automatically add charset=utf-8, that may be returned back in response.

You can assert them as follow:

Assert.assertEquals(response.header("Content-Type"),("application/json; charset=utf-8"));

or to disable automatic charset=utf-8 in request header:

RestAssured.config = RestAssured.config(config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文