简体   繁体   中英

Print response body when statusCode assert fails with restassured

I'm using Hamcrest to unit test a REST API.

When I send a request, I often check for a 200 status code like this :

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 :

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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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