java - 将来自 REST Assured 测试的请求和响应详细信息添加到 Surefire 报告

标签 java maven junit maven-surefire-plugin rest-assured

Rest Assured允许编写 R​​EST 端点的测试。它具有的有用功能之一是能够 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\": 1\n" +
        "}";
    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",
    "body": "bar",
    "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您可以将其配置为写入任何默认为 System.outPrintStream

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

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

关于java - 将来自 REST Assured 测试的请求和响应详细信息添加到 Surefire 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876826/

相关文章:

java - 如何防止重新启动正在运行的服务 - Android

java - 显示贝壳排序过程

database - 在 Spring Boot 中安装不带数据库的 Sonar

selenium - 使用 JUnit 运行 cucumber 测试时出现 java.lang.NoClassDefFoundError 异常

java - 找到 "stale element reference"可见的元素后立即发生 "wait.until(ExpectedConditions.visibilityOfElementLocated"错误

java - Spring 集成是否为其组件提供任何监控/时间性能实用程序?

java - 我可以在测试运行时跳过 JUnit 测试吗?

android - 执行 Android JUnit 测试时出现 NoClassDefFoundError

eclipse - 如何在 Maven 中解析 'Offline/Missing artifact org.geotools:gt-shapefile:jar:17-SNAPSHOT'?

java - Maven 没有从 settings.xml 中为存储库选择用户名