Press "Enter" to skip to content

Logging to Disk with REST Assured

A common question that pops up now and again is how to configure REST Assured logs to be written to disk instead of printed to the console. The intention of this blog post is to demonstrate how this can be achieved.

But first let’s look at how to use logging in REST Assured:

Logging to Console

REST Assured has something called a Request- and ResponseSpecification and you log the contents of these individually. This is why you often see code like this:

The first call to log().all() says that we want to log everything in the request specification and the second call to log().all() instructs REST Assured to log everything in the response specification (after the call has been made). The results can look like this:

We can also decide not to log the request specification at all and only log the response body like this:

which will log this to the console:

Note that by default the body is pretty-printed if it’s in a format that REST Assured understands (such as XML, JSON or HTML). You can easily disable this though:

which will return the body as is:

This is all fine, but what if you don’t want to log to the console but instead logging to a file?

Logging to a File

REST Assured comes with a lot of configuration options. By default it provides sane defaults but you can customize a lot of things in REST Assured if required. One of the things you can customize is how logging behaves using the LogConfig class. The LogConfig allows you to specify a default java.io.PrintStream that is used for logging when calling e.g. log().all(). If you haven’t specified a PrintStream explicitly then REST Assured will default to the PrintStream defined by System.out, i.e. it’ll print the logs to the console. But let’s see how we can change this to write a file instead:

This will log everything to the file /tmp/logging.txt. Note that in this example we’re using the WriterOutputStream class from Apache Commons IO.

Using a JUnit Rule

Typically you probably want to configure the use of file logging once and then have it done with. One way to do this would be to create a Rule in JUnit4 or an extension in JUnit 5. Here we’re going to explore the first option and create a custom JUnit rule. It may be defined like this:

What this rule does is to create a log file for each test case in the given file. Let’s see how it can be used:

Now all calls to log() will be logged to disk. In this case two files will be generated in the /tmp/rest-assured-logs folder, example1.log and example2.log which contains the response logs for each respective test. It only contains the response logs in this example since we’ve chosen not to add log() to the RequestSpecification. There’s of course room for improvement to the WriteLogsToDisk rule, for example we could automatically remove files which are empty (i.e. no REST Assured logs where recorded for a particular test). You can have a look at an example test here and the code for WriteLogsToDisk here.

Conclusion

As you can see REST Assured is quite flexible and you can configure it to achieve most use cases, logging to disk being one example. If you haven’t done so already, feel free to try it out. Happy testing!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *