Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add request and response detail from REST Assured test to Surefire report

Rest Assured allows to write tests of REST endpoints. One of the useful features it has is the ability to log the request and response in case of a failed test so you can check what was sent to the REST service.

Request example

An example of a failing test which logs both the request and the response

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

This test fails because the returned userId is 1. This is what's printed on the console when executing the test.

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]

The problem

It is a great feature as it allows to report more contextful bug reports. Under the hood it uses a LogConfig which you can configure to write to any PrintStream defaulting to System.out.

The Maven Surefire plugin reports errors on two files (text and XML). But on these files it only includes the content from the exception thrown by the test method. So the detail from the report looks like this:

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]

Which is nice but could be nicer.

The question

Should be pretty obvious by now.

How can I include the request and response detail from REST Assured on the Surefire report?

like image 393
sargue Avatar asked Oct 31 '22 01:10

sargue


1 Answers

I found an answer here: Set restAssured to log all requests and responses globally

I start by importing log filters, like this

import io.restassured.filter.log.*;

Then I add the filters to the given() chain, like this

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

I could then see the raw request and response in my test output directly.

like image 161
Daniel Watrous Avatar answered Nov 11 '22 08:11

Daniel Watrous