加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 将REST Assured测试中的请求和响应详细信息添加到Surefi

发布时间:2020-12-15 02:15:14 所属栏目:Java 来源:网络整理
导读:Rest Assured允许编写REST端点的测试.它具有的一个有用功能是 log the request and response in case of a failed test的功能,因此您可以检查发送到REST服务的内容. 请求示例 记录请求和响应的失败测试示例 import io.restassured.RestAssured;import org.ju
Rest Assured允许编写REST端点的测试.它具有的一个有用功能是 log the request and response in case of a failed test的功能,因此您可以检查发送到REST服务的内容.

请求示例

记录请求和响应的失败测试示例

import io.restassured.RestAssured;
import org.junit.Before;
import org.junit.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class TestRestAssured {
  @Before
  public void setUp() {
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
  }

  @Test
  public void exampleFailingTest() {
    String json = "{n" +
        "  "title": "foo",n" +
        "  "body": "bar",n" +
        "  "userId": 1n" +
        "}";
    given()
        .contentType("application/json; charset=UTF-8")
        .body(json)
        .post("http://jsonplaceholder.typicode.com/posts")
        .then()
        .body("userId",equalTo(2));
  }
}

此测试失败,因为返回的userId为1.这是执行测试时在控制台上打印的内容.

Request method: POST
Request URI:    http://jsonplaceholder.typicode.com/posts
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Multiparts:     <none>
Headers:        Accept=*/*
                Content-Type=application/json; charset=UTF-8
Cookies:        <none>
Body:
{
    "title": "foo","body": "bar","userId": 1
}

HTTP/1.1 201 Created
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Vary: Origin,X-HTTP-Method-Override,Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Content-Length: 65
Etag: W/"41-+JqcAMRWkzuXd+uFDZdzIA"
Date: Fri,17 Jun 2016 08:18:59 GMT
Via: 1.1 vegur

{
    "title": "foo","userId": 1,"id": 101
}

java.lang.AssertionError: 1 expectation failed.
JSON path userId doesn't match.
Expected: <2>
  Actual: 1


    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [rest of the stacktrace]

问题

这是一个很棒的功能,因为它允许报告更多上下文错误报告.在引擎盖下它使用LogConfig,您可以配置为写入任何PrintStream默认为System.out.

Maven Surefire plugin报告两个文件(文本和XML)的错误.但是在这些文件中,它只包含测试方法抛出的异常中的内容.因此报告中的详细信息如下所示:

java.lang.AssertionError: 1 expectation failed.
JSON path userId doesn't match.
Expected: <2>
  Actual: 1


    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [rest of the stacktrace]

哪个好,但可能更好.

这个问题

现在应该很明显了.

如何在Surefire报告中包含REST Assured的请求和响应详细信息?

解决方法

我在这里找到了答案: Set restAssured to log all requests and responses globally

我首先导入日志过滤器,像这样

import io.restassured.filter.log.*;

然后我将过滤器添加到given()链中,就像这样

RequestSpecification myRequest = RestAssured.given()
                                            .filter(new ResponseLoggingFilter())
                                            .filter(new RequestLoggingFilter());

然后我可以直接在测试输出中看到原始请求和响应.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读