java.lang.IllegalArgumentException:模拟记录器时,不能将最终类类org.slf4j.LoggerFactory子类化

时间:2018-02-19 14:36:26

标签: java logging junit mockito

当我试图模拟记录器时,我得到了上述异常。

我想验证日志。

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class MedSlaveImpl implements MedSlaveInt {
    private static final Logger logger = LoggerFactory.getLogger(MedSlaveImpl.class);

@Override
    public void enterMode() {
        logger.info("muting the Manager");

    }

}

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.powermock.modules.junit4.PowerMockRunner;

import uk.org.lidalia.slf4jtest.TestLoggerFactory;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/context-test.xml", "/context.xml" })
@TestPropertySource("/config.properties")
@ActiveProfiles("mediator")
@PrepareForTest({ MedSlaveImpl.class, LoggerFactory.class })
public class MedModeTest {


    // For mediator
    @Test
    public void testStartService() throws Exception {


        mockStatic(LoggerFactory.class);
        Logger logger = mock(Logger.class);
        when(LoggerFactory.getLogger(any(Class.class))).thenReturn(logger);
        // TestLogger logger =
        // TestLoggerFactory.getTestLogger(MedSlaveImpl.class);

        mediatorImpl.enterMode();
        verify(logger).info("muting the Manager");
        // assertThat(logger.getLoggingEvents(),is(asList(info("muting theManager"))));
    }

    @After
    public void clearLoggers() {
        TestLoggerFactory.clear();
    }

}

我花了一整天的时间用不同的方法验证日志。喜欢使用

TestLogger记录器,Mockitto,powermock。最后我得到了不同的例外。

对于目前的方法,我得到了上述例外。

非常感谢您的建议。谢谢您的帮助

2 个答案:

答案 0 :(得分:0)

你有几个选择。一个是遵循Darren的建议并捕获记录的输出并进行检查。另一种方法是提供您自己的LoggerFactory,根据您的喜好生成记录器。请注意,SFL4J是一个API,所以这是可行的。然而,对于你想要完成的事情来说,这似乎是很多工作。

答案 1 :(得分:0)

没有模仿记录器工厂的解决方案。

有延迟加载记录器的方法:

public class MedSlaveImpl implements MedSlaveInt {
    private static Logger logger;

    // Package visible to override it in the tests.
    Logger getLogger() {
        if (logger == null) {
            logger = LoggerFactory.getLogger(MedSlaveImpl.class);
        }
        return logger;
    }

    @Override
    public void enterMode() {
        // All calls to the logger via getLogger() method.
        getLogger().info("muting the Manager");
    }
}

您的测试中包含已覆盖getLogger()方法的服务类实例:

@Test
public void testStartService() throws Exception {
    //Setup
    final Logger mockedLogger = mock(Logger.class);

    final MedSlaveImpl service = new MedSlaveImpl() {
        @Override
        Logger getLogger() {
            return mockedLogger;
        }
    }

    // When
    service.enterMode();

    // Then
    verify(mockedLogger).info("muting the Manager");
}