A Junit rule to turn test logging on/off

Testing methods which log exceptions can result in a messy build log, peppered with stack traces and error messages, without any obvious way to discern whether these errors are intentionally triggered by the tests.

Example:

The above test will succeed but also produce the following output in the logs:

SEVERE: error
java.lang.IllegalArgumentException: boom
at com.company.Item.function(Item.java:12)
at com.company.ItemTest.testFunction(ItemTest.java:12)

The above output is in this case undesirable and can be hidden by using a junit rule which will run before the test to set the logging level to OFF, and then back again to it’s original level once the test is finished.

Output becomes:
Process finished with exit code 0

Advertisements

One thought on “A Junit rule to turn test logging on/off

  1. Julissa

    You share interesting things here. I think that your blog can go viral easily, but you must give it initial boost and
    i know how to do it, just search in google (with quotes) for – “mundillo traffic increase make your website go viral”

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s