I'm wondering what is the easiest and most concise way to mock org.apache.log4j with powermock (and mockito).
I have tried a few approaches (which I won't illustrate here) but have not yet found a way to achieve what I want. I have created a simple class to test below and I want to call the run method and verify that the log.info message has been called. How do I do this? I'm sure it's quite easy when you know how!
(I'm using @Rule as I want to run under spring test but that should make no difference).
Thanks a millon for the correct code.
import org.apache.log4j.Logger;
import org.junit.Rule;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
public class MockLog4JTest
{
@Rule
public PowerMockRule rule = new PowerMockRule();
private static class ClassUnderTest
{
private static Logger logger = Logger.getLogger(ClassUnderTest.class);
public void run() {
logger.info("Hello.");
}
}
@Test
public void testLog()
{
ClassUnderTest classUnderTest = new ClassUnderTest();
classUnderTest.run();
}
}