Logger with mockito in java我的 JUnit 类
我正在尝试使用 mockito 验证记录器消息。
但是,我无法运行我的 junit 类来覆盖所有代码行。
你知道为什么吗?
我的代码:
public class App {
private static final Logger LOGGER = Logger.getLogger(App.class);
public List<String> addToListIfSizeIsUnder3(final List<String> list, final String value) {
if (list == null) {
LOGGER.error("A null list was passed in");
return null;
}
if (list.size() < 3) {
list.add(value);
} else {
LOGGER.debug("The list already has {} entries"+ list.size());
}
return list;
}
}
我的 JUnit 类
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class AppTest {
private App uut;
@Mock
private Appender mockAppender;
@Captor
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
@Before
public void setup() {
uut = new App();
Logger root = Logger.getRootLogger();
root.addAppender(mockAppender);
root.setLevel(Level.INFO);
}
/**
* I want to test with over 3 elements.
*/
@Test
public void testWithOver3Element() {
List<String> myList = new ArrayList<String>();
myList.add("value 1");
myList.add("value 2");
myList.add("value 3");
myList.add("value 4");
List<String> outputList = uut.addToListIfSizeIsUnder3(myList, "some value");
Assert.assertEquals(4, outputList.size());
Assert.assertFalse(myList.contains("some value"));
try {
verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());
} catch (AssertionError e) {
e.printStackTrace();
}
LoggingEvent loggingEvent = captorLoggingEvent.getAllValues().get(0);
Assert.assertEquals("The list already has {} entries", loggingEvent.getMessage());
Assert.assertEquals(Level.DEBUG, loggingEvent.getLevel());
}
}
错误:
Wanted 但未调用:mockAppender.doAppend();-> AppTest.testWithOver3Element(AppTest.java:52) 实际上,与此模拟的交互为零。
at AppTest.testWithOver3Element(AppTest.java:52) at sun.reflect.nativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.delegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statement.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statement.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) atorg.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)