简体   繁体   English

当 statusCode 断言失败并重新保证时打印响应正文

[英]Print response body when statusCode assert fails with restassured

I'm using Hamcrest to unit test a REST API.我正在使用 Hamcrest 对 REST API 进行单元测试。

When I send a request, I often check for a 200 status code like this :当我发送请求时,我经常检查200状态代码,如下所示:

public void myTest() {
    url = "route/to/my/rest/api/";
    secured().when().get(url).then().statusCode(200);
}

But when I get a wrong code status, I only get an assertion error.但是当我得到错误的代码状态时,我只会得到一个断言错误。 Is there a way to automatically dump the response body (which contains the error) when the status code doesn't match ?当状态代码不匹配时,有没有办法自动转储响应正文(其中包含错误)?

The secured() method : secured()方法:

public RequestSpecification secured() {
    return given().header("Authorization", "Bearer " + getAuth());
}

As I mentioned in the comments I used the following正如我在评论中提到的,我使用了以下内容

secured().when().post(url).then().log().ifValidationFails(LogDetail.BODY).statusCode(200);

You can find the source in the documentation您可以在文档中找到源

当测试失败时,您可以向断言添加一条消息:

.statusCode(describedAs("The test fails because ...", is(200)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM