Re: Best practices for unit testing that logging is correct done
Here's an example of pulling in a test jar dependency: < https://github.com/apache/logging-log4j2/blob/master/log4j-core-its/pom.xml#L64-L69>. Just add the tag as well. https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.logging.log4j%22%20AND%20a%3A%22log4j-core%22 Note the tests.jar artifacts. On 22 February 2018 at 05:45, Atle Toklewrote: > Thank you. This is exactly what I'm looking for. > > I have tried to use it, but don't see any way I can include it into my > maven test dependency. Do I have to copy all the stuff I need and make a > local test-library, or is it a maven GAV reference that I can include in my > test-scope to get it. > > Atle > > 2018-02-20 22:57 GMT+01:00 Matt Sicker : > > > As for a random example: > > > > Config file: > > https://github.com/apache/logging-log4j2/blob/master/ > > log4j-core/src/test/resources/log4j-customLevel.xml > > > > Test class: > > https://github.com/apache/logging-log4j2/blob/master/ > > log4j-core/src/test/java/org/apache/logging/log4j/core/ > > CustomLevelsTest.java > > > > JUnit rule: > > https://github.com/apache/logging-log4j2/blob/master/ > > log4j-core/src/test/java/org/apache/logging/log4j/junit/ > > LoggerContextRule.java > > > > ListAppender: > > https://github.com/apache/logging-log4j2/blob/master/ > > log4j-core/src/test/java/org/apache/logging/log4j/test/ > > appender/ListAppender.java > > > > On 20 February 2018 at 15:53, Matt Sicker wrote: > > > > > If you add log4j-core:test (the test jar), there's a ListAppender built > > in > > > that we use extensively to verify logging messages in various > scenarios. > > If > > > you add the list appender to your log4j2-test.xml file, then it'll take > > > precedence over a normal log4j2.xml file. > > > > > > You can probably take a look at almost any random config file in > > > log4j-core/src/test/resources/ and you'll find a list appender being > > used. > > > Then you can use the ListAppender class itself to locate that list, > > though > > > there's also the LoggerContextRule for JUnit 4.x that you can also use > > (see > > > one of the numerous unit tests in log4j-core for examples). > > > > > > On 20 February 2018 at 04:44, Atle Tokle wrote: > > > > > >> I would like to know suggestions for best practices for unit-testing > > that > > >> logging is correctly performed in the code. > > >> > > >> I use Mockito, and have tried a few aproaches which I'm not pleased > > with. > > >> 1. I have tried static mock with Powermockito, but PowerMockito have > > >> several bad effects and screws up code coverage report > > >> 2. I have tried configure logging to console and capture System.out, > but > > >> that also is clumsy and have several bad effects, and is fragile if > > >> someone > > >> changes log4j-test.xml for any reason. > > >> > > >> Is it possible to programatic setup so logger created in code is > backed > > by > > >> a Mockito Mock or Capture? > > >> > > >> Best Regards > > >> Atle > > >> > > > > > > > > > > > > -- > > > Matt Sicker > > > > > > > > > > > -- > > Matt Sicker > > > -- Matt Sicker
Re: Best practices for unit testing that logging is correct done
As for a random example: Config file: https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/test/resources/log4j-customLevel.xml Test class: https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/test/java/org/apache/logging/log4j/core/CustomLevelsTest.java JUnit rule: https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/test/java/org/apache/logging/log4j/junit/LoggerContextRule.java ListAppender: https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/ListAppender.java On 20 February 2018 at 15:53, Matt Sickerwrote: > If you add log4j-core:test (the test jar), there's a ListAppender built in > that we use extensively to verify logging messages in various scenarios. If > you add the list appender to your log4j2-test.xml file, then it'll take > precedence over a normal log4j2.xml file. > > You can probably take a look at almost any random config file in > log4j-core/src/test/resources/ and you'll find a list appender being used. > Then you can use the ListAppender class itself to locate that list, though > there's also the LoggerContextRule for JUnit 4.x that you can also use (see > one of the numerous unit tests in log4j-core for examples). > > On 20 February 2018 at 04:44, Atle Tokle wrote: > >> I would like to know suggestions for best practices for unit-testing that >> logging is correctly performed in the code. >> >> I use Mockito, and have tried a few aproaches which I'm not pleased with. >> 1. I have tried static mock with Powermockito, but PowerMockito have >> several bad effects and screws up code coverage report >> 2. I have tried configure logging to console and capture System.out, but >> that also is clumsy and have several bad effects, and is fragile if >> someone >> changes log4j-test.xml for any reason. >> >> Is it possible to programatic setup so logger created in code is backed by >> a Mockito Mock or Capture? >> >> Best Regards >> Atle >> > > > > -- > Matt Sicker > -- Matt Sicker
Re: Best practices for unit testing that logging is correct done
If you add log4j-core:test (the test jar), there's a ListAppender built in that we use extensively to verify logging messages in various scenarios. If you add the list appender to your log4j2-test.xml file, then it'll take precedence over a normal log4j2.xml file. You can probably take a look at almost any random config file in log4j-core/src/test/resources/ and you'll find a list appender being used. Then you can use the ListAppender class itself to locate that list, though there's also the LoggerContextRule for JUnit 4.x that you can also use (see one of the numerous unit tests in log4j-core for examples). On 20 February 2018 at 04:44, Atle Toklewrote: > I would like to know suggestions for best practices for unit-testing that > logging is correctly performed in the code. > > I use Mockito, and have tried a few aproaches which I'm not pleased with. > 1. I have tried static mock with Powermockito, but PowerMockito have > several bad effects and screws up code coverage report > 2. I have tried configure logging to console and capture System.out, but > that also is clumsy and have several bad effects, and is fragile if someone > changes log4j-test.xml for any reason. > > Is it possible to programatic setup so logger created in code is backed by > a Mockito Mock or Capture? > > Best Regards > Atle > -- Matt Sicker