16

I'd like to know if there is a way of logging all the calls and responses (url + payload) processed by restassured.

THe finality would be to have a "debug" test log with all the calls and traffic logged.

Of course I could issue calls to the logger in my own code, but I'd prefear to set this behavour globally and not to add logger calls in all my test methods.

Thanks for any pointers

devlearn
  • 1,725
  • 2
  • 17
  • 30

4 Answers4

19

I am posting an example:

 Response response = given().
                    queryParam("apiKey", "abc123").
                    queryParam("code", code).
                    queryParam("type", type).
                    contentType("application/json").
                    log().all().
                    when().
                    get(url).
                    then().
                    contentType("application/json").
                    statusCode(200).
                    extract().response();
Mircea Stanciu
  • 3,675
  • 3
  • 34
  • 37
12

Set log().all() for your response and it will be okay.

Gergely A.
  • 404
  • 3
  • 6
1

Sorry dumb question (or issue with my vision), everything is documented under:

devlearn
  • 1,725
  • 2
  • 17
  • 30
0

Here are the steps to log to a log file and configure globally to log both request and response details for all the tests

create static printstream object

private static PrintStream logps;
        try {
        // fileoutputstream can be opened in append mode to append the logs every time
        // we run
        // printstream is enabled for autoflush
        logps = new PrintStream(new FileOutputStream("src/test/resources/logfile.txt", true), true);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

In beforeall static method, apply the default implemented request and response logging filters and enable them to use print stream

RestAssured.filters(new RequestLoggingFilter(logps), new ResponseLoggingFilter(logps));

Now we need not specify log() method individually for requests and responses and also the logs will be sent to the log file