我尝试用mokito验证记录器消息.
但是,我无法运行我的junit类来覆盖所有代码行.你是这个原因吗?
我的代码:
public class App { private static final Logger LOGGER = Logger.getLogger(App.class); public ListaddToListIfSizeIsUnder3(final List 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 ArgumentCaptorcaptorLoggingEvent; @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 myList = new ArrayList (); myList.add("value 1"); myList.add("value 2"); myList.add("value 3"); myList.add("value 4"); List 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()); } }
错误:
通缉但未调用:mockAppender.doAppend(); - >在AppTest.testWithOver3Element(AppTest.java:52)实际上,与这个模拟没有交互.
在app.estWatOver3Element(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.来自org.junit.internal.runners.model.ReflectiveCallable.run的org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)中的lang.reflect.Method.invoke(未知来源)(ReflectiveCallable.java: 12)org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)org.junit.internal.runners .statements.RunBefores.evaluate(RunBefores.java:26)位于org.junit.runners的org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271).BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)org.junit. org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)上的orners.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)org中的runners.ParentRunner $ 1.schedule(ParentRunner.java:63)位于org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run的org.junit.runners.ParentRunner.run(ParentRunner.java:309)的.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)(JUnit45AndHigherRunnerImpl.java: 37)在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run上的org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)(JUnit4TestReference.java:86)org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org位于org.eclipse.jdt的org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)的.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) .internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
小智.. 11
您可以采取以下措施来改进代码:
切换到slf4j.这将允许您编写一个接口代码并且不知道封面下的日志记录实现(在您的情况下为apache log4j).
切换到slf4j将允许您在传递给日志框架时不必连接字符串 - 示例:
此声明:LOGGER.debug("列表已有{}条目"+ list.size());
可以这样写:LOGGER.debug("列表已经有{}条目",list.size());
这样做的另一个好处是可以使字符串文字中的占位符实际工作.
您正试图通过Logging框架间接断言和捕获对象的调用.这将是脆弱的并且容易出错,因为您永远不知道在Logging框架内部将进行哪些调用.嘲笑你的直接依赖.
不要测试日志记录语句.它不是完全可见的类行为,它使测试变得脆弱和复杂.另外,正如您所说的那样使用ArrayList(即语言的一部分)来处理日志语句允许它们被完全运用并且它们将信息输出到控制台,这可能有助于调试失败的测试.脆弱的一个例子是,如果你更改日志语句以添加更多信息,或者你可以在方法中添加另一个日志语句,那么这个测试可能没有任何理由.至少不要断言被调用的次数,因为这将非常脆弱.
所有这些都说,如果你必须测试与Logging框架的交互 - 这里是运行的代码的修改版本并提供相同的功能.这基本上是改进列表中的选项#3 -
package com.spring.mockito; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.apache.log4j.Logger; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import java.util.Arrays; import java.util.List; @RunWith(MockitoJUnitRunner.class) public class AppTest { // create a mock of the logger @Mock private Logger logger; private App uut; // Not needed - dont test something that gets called through something else // @Captor // private ArgumentCaptorcaptorLoggingEvent; @Before public void setup() { // spy the class under test so we can override the logger method to return our mock logger uut = spy(new App()); when(uut.logger()).thenReturn(logger); // Not needed test with the mock directly. // Logger root = Logger.getRootLogger(); // root.addAppender(mockAppender); // root.setLevel(Level.DEBUG); } /** * I want to test with over 3 elements. */ @Test public void testWithOver3Element() { List myList = Arrays.asList("value 1", "value 2", "value 3", "value 4"); List outputList = uut.addToListIfSizeIsUnder3(myList, "some value"); Assert.assertEquals(4, outputList.size()); Assert.assertFalse(myList.contains("some value")); verify(logger, times(1)).debug("The list already has {} entries4"); // not needed // 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()); } public static class App { private static final Logger LOGGER = Logger.getLogger(App.class); public List addToListIfSizeIsUnder3(final List 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 { // if you use slf4j this concatenation is not needed logger().debug("The list already has {} entries" + list.size()); } return list; } // make a package private method for testing purposes to allow you to inject a mock Logger logger() { return LOGGER; } } }
您还可以查看PowerMockito等软件包来模拟静态 - 但仅在绝对需要的情况下.
希望这可以帮助.
您可以采取以下措施来改进代码:
切换到slf4j.这将允许您编写一个接口代码并且不知道封面下的日志记录实现(在您的情况下为apache log4j).
切换到slf4j将允许您在传递给日志框架时不必连接字符串 - 示例:
此声明:LOGGER.debug("列表已有{}条目"+ list.size());
可以这样写:LOGGER.debug("列表已经有{}条目",list.size());
这样做的另一个好处是可以使字符串文字中的占位符实际工作.
您正试图通过Logging框架间接断言和捕获对象的调用.这将是脆弱的并且容易出错,因为您永远不知道在Logging框架内部将进行哪些调用.嘲笑你的直接依赖.
不要测试日志记录语句.它不是完全可见的类行为,它使测试变得脆弱和复杂.另外,正如您所说的那样使用ArrayList(即语言的一部分)来处理日志语句允许它们被完全运用并且它们将信息输出到控制台,这可能有助于调试失败的测试.脆弱的一个例子是,如果你更改日志语句以添加更多信息,或者你可以在方法中添加另一个日志语句,那么这个测试可能没有任何理由.至少不要断言被调用的次数,因为这将非常脆弱.
所有这些都说,如果你必须测试与Logging框架的交互 - 这里是运行的代码的修改版本并提供相同的功能.这基本上是改进列表中的选项#3 -
package com.spring.mockito; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.apache.log4j.Logger; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import java.util.Arrays; import java.util.List; @RunWith(MockitoJUnitRunner.class) public class AppTest { // create a mock of the logger @Mock private Logger logger; private App uut; // Not needed - dont test something that gets called through something else // @Captor // private ArgumentCaptorcaptorLoggingEvent; @Before public void setup() { // spy the class under test so we can override the logger method to return our mock logger uut = spy(new App()); when(uut.logger()).thenReturn(logger); // Not needed test with the mock directly. // Logger root = Logger.getRootLogger(); // root.addAppender(mockAppender); // root.setLevel(Level.DEBUG); } /** * I want to test with over 3 elements. */ @Test public void testWithOver3Element() { List myList = Arrays.asList("value 1", "value 2", "value 3", "value 4"); List outputList = uut.addToListIfSizeIsUnder3(myList, "some value"); Assert.assertEquals(4, outputList.size()); Assert.assertFalse(myList.contains("some value")); verify(logger, times(1)).debug("The list already has {} entries4"); // not needed // 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()); } public static class App { private static final Logger LOGGER = Logger.getLogger(App.class); public List addToListIfSizeIsUnder3(final List 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 { // if you use slf4j this concatenation is not needed logger().debug("The list already has {} entries" + list.size()); } return list; } // make a package private method for testing purposes to allow you to inject a mock Logger logger() { return LOGGER; } } }
您还可以查看PowerMockito等软件包来模拟静态 - 但仅在绝对需要的情况下.
希望这可以帮助.