运行单元测试时未调用Mockito Mock log4j appender

hc8w905p  于 2023-08-05  发布在  其他
关注(0)|答案(3)|浏览(105)

我正在尝试对代码中生成的日志语句进行单元测试。我使用slfj、log4j和Mockito。我正在使用类似的代码如下从博客在http://bloodredsun.com/2010/12/09/checking-logging-in-unit-tests/
当我运行测试时,它抛出异常,说在第行有0个调用:
第一个月

错误信息

被通缉但未被援引:mockAppender.doAppend();-> at testClass.testLogAdviceAfterReturning(DpsOpsLoggerTest2.java:94)实际上,与此mock的交互为零。
我看到控制台上打印的日志。恳请您的帮助。

@RunWith(MockitoJUnitRunner.class)
public class ExampleThatLogsTest {

    @Mock
    private Appender mockAppender;
    @Captor
    private ArgumentCaptor captorLoggingEvent;

    @Before
    public void setup() {
        LogManager.getRootLogger().addAppender(mockAppender);
    }

    @After
    public void teardown() {
        LogManager.getRootLogger().removeAppender(mockAppender);
    }

    @Test
    public void shouldConcatAndLog() {
        //given
        ExampleThatLogs example = new ExampleThatLogs();
        //when
        String result = example.concat("foo", "bar");
        //then
        assertEquals("foobar", result);

        verify(mockAppender).doAppend(captorLoggingEvent.capture());
        LoggingEvent loggingEvent = captorLoggingEvent.getValue();
        //Check log level
        assertThat(loggingEvent.getLevel(), is(Level.INFO));
        //Check the message being logged
        assertThat(loggingEvent.getRenderedMessage(), 
            is("String a:foo, String b:bar"));
    }
}

字符串

izkcnapc

izkcnapc1#

我试着模仿你的情况,在我的结束它是工作的罚款
//日志工具

public class LogUtil{

        final static Logger logger = Logger.getLogger(LogUtil.class);

        public static Log`enter code here`ger getLogger()
        {
            return logger;
        }

字符串
//class

public class RunMe {

        public String runMe(String parameter) {

            LogUtil.getLogger().info("This is info : " + parameter);
            return "In runner " + parameter;

        }
    }


//单元测试

@RunWith(MockitoJUnitRunner.class)
public class LoggerTest {

    @Mock
    private Appender mockAppender;
    @Captor
    private ArgumentCaptor captorLoggingEvent;

    @Before
    public void setup() {

        LogUtil.getLogger().addAppender(mockAppender);
        }

    @Test
    public void shouldConcatAndLog() {

        RunMe runner=new RunMe();
        String result=runner.runMe("XYZ");
        assertEquals("In runner XYZ",result);
       verify(mockAppender).doAppend((LoggingEvent) captorLoggingEvent.capture());
      LoggingEvent logevent= (LoggingEvent) captorLoggingEvent.getValue();
      assertThat(logevent.getLevel(), is(Level.INFO));
    }

    @After
    public void tearDown() {
        LogUtil.getLogger().removeAllAppenders();
    }
}

oprakyz7

oprakyz72#

我知道这有点过时,但我也在努力。我在被测类的DEBUG级别记录语句。我在logback.xml中为被测类设置的配置被设置为INFO。将我的日志语句更改为INFO允许测试通过。此外,我还阅读了this Github的帖子,它非常简洁,是一个测试日志输出的干净实现。希望其他人会发现它有用。

ni65a41a

ni65a41a3#

使用This verify(mockAppender,atLeastOnce()).doAppend(captorLoggingEvent.capture());在verify方法中的第二个参数告诉你要模拟多少次,所以在你的情况下它是零你也可以参考这个:-https:www.baeldung.com/mockito-verify

相关问题