Junit testing log statements
<p>I have a java 8 app with a class foo like this:</p>

<pre><code>import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foo {
    private final Logger log = LoggerFactory.getLogger(Foo.class);

    public String something() {
        log.info("doing foo test");
        return "test";
    }
}
</code></pre>

<p>Which I am writing a JUnit (Junit 4.11) test case for like this:</p>

<pre><code>public class FooTest {
    private Foo foo;

    @Before
    public void setUp() throws Exception {
        foo = new Foo();
    }

    @Test
    public void testSomething() {
        String result = foo.something();
        assertEquals(result,"test");
    }
}
</code></pre>

<p>My objective is to write a test case that tests the <code>something</code> method for it's return value <strong>AND</strong> the logging statement to make sure something is being logged. I've spent hours scouring the web to figure out how to setup the junit to test the logging statement. I've tried <a href="http://giordano.scalzo.biz/2009/10/21/no-more-excuses-junit-testing-log-messages/" rel="noreferrer">this</a>, <a href="http://codebyexample.info/2012/03/31/how-log4j-and-junit-became-friends/" rel="noreferrer">this</a>, <a href="http://bloodredsun.com/2010/12/09/checking-logging-in-unit-tests/" rel="noreferrer">this</a> and <a href="http://blog.diabol.se/?p=474" rel="noreferrer">this</a> method to no avail.</p>

<p>I don't know if it's something I'm doing wrong or what. But here's my code based on the last example:</p>

<pre><code>public class FooTest {
    private Foo foo;
    @Mock private Appender appender;
    @Captor private ArgumentCaptor captor;

    @Before
    public void setUp() throws Exception {
        foo = new Foo();
        MockitoAnnotations.initMocks(this);
        Logger.getRootLogger().addAppender(appender);
    }

    @After
    public void tearDown() throws Exception {
        Logger.getRootLogger().removeAllAppenders();
    }

    @Test
    public void testSomething() {
        String result = foo.something();
        assertEquals(result,"test");
        verify(appender).doAppend(((LoggingEvent) captor.capture()));
        LoggingEvent loggingEvent = (LoggingEvent) captor.getValue();
        assertEquals(loggingEvent.getRenderedMessage(), "doing foo test");
    }
}
</code></pre>

<p>But when I run this I get the following error:</p>

<pre><code>Wanted but not invoked:
appender.doAppend(&lt;Capturing argument&gt;);
-&gt; at &lt;package&gt;.testSomething(FooTest.java:22)
Actually, there were zero interactions with this mock.
</code></pre>

<p>Is there an easier way to accomplish what I want? If this is the best way then what am I doing wrong?</p>