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 <ato...@gmail.com> 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 <boa...@gmail.com>

Reply via email to