Java 将 restAssured 设置为全局记录所有请求和响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44259851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set restAssured to log all requests and responses globally
提问by MuchHelping
I want to enable logging for all restAssured responses and requests by default.
我想默认为所有 restAssured 响应和请求启用日志记录。
Here's what I do:
这是我所做的:
RestAssured.requestSpecification = new RequestSpecBuilder().
setBaseUri("api").
setContentType(ContentType.JSON).
build().
log().all();
RestAssured.responseSpecification = new ResponseSpecBuilder().
build().
log().all();
requestSpecification works alright, but with responseSpecification I get:
requestSpecification 工作正常,但使用 responseSpecification 我得到:
Cannot configure logging since request specification is not defined. You may be misusing the API.
由于未定义请求规范,因此无法配置日志记录。您可能在滥用 API。
I really don't want to use log().all() after each then.
我真的不想在每次之后都使用 log().all() 。
采纳答案by RocketRaccoon
Add logging filters to RestAssured defaults, see filtersand defaults.
将日志过滤器添加到 RestAssured 默认值,请参阅过滤器和默认值。
To create a filter you need to implement the io.restassured.filter.Filter interface. To use a filter you can do:
given().filter(new MyFilter()). ..There are a couple of filters provided by REST Assured that are ready to use:
1. io.restassured.filter.log.RequestLoggingFilter: A filter that'll print the request specification details.
2. io.restassured.filter.log.ResponseLoggingFilter: A filter that'll print the response details if the response matches a given status code.
3. io.restassured.filter.log.ErrorLoggingFilter: A filter that'll print the response body if an error occurred (status code is between 400 and 500)
要创建过滤器,您需要实现 io.restassured.filter.Filter 接口。要使用过滤器,您可以执行以下操作:
given().filter(new MyFilter())。..REST Assured 提供了几个
可供使用的过滤器:1. io.restassured.filter.log.RequestLoggingFilter:将打印请求规范详细信息的过滤器。
2. io.restassured.filter.log.ResponseLoggingFilter:如果响应匹配给定状态代码,将打印响应详细信息的过滤器。
3. io.restassured.filter.log.ErrorLoggingFilter:如果发生错误将打印响应正文的过滤器(状态代码在 400 和 500 之间)
Any filter could be added to request, spec or global defaults:
任何过滤器都可以添加到请求、规范或全局默认值中:
RestAssured.filters(..); // List of default filters
RestAssured.filters(..); // 默认过滤器列表
回答by ?tefan Tislari
I think you need to see the logs then test fails, in this case just use this configuration from rest assured:
我认为您需要查看日志然后测试失败,在这种情况下,请放心使用此配置:
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
回答by Luiz Gustavo
Put this line of code on your @BeforeClass
method and every given call will create a log just like using log.all() after every given:
把这行代码放在你的@BeforeClass
方法上,每个给定的调用都会创建一个日志,就像在每次给定之后使用 log.all() 一样:
RestAssured.filters(new RequestLoggingFilter(), new ResponseLoggingFilter());
Rest-Assured project:
https://github.com/rest-assured/rest-assured/blob/master/rest-assured/src/main/java/io/restassured/filter/log/RequestLoggingFilter.java
Rest-Assured 项目:https:
//github.com/rest-assured/rest-assured/blob/master/rest-assured/src/main/java/io/restassured/filter/log/RequestLoggingFilter.java