【问题标题】:java.lang.IllegalArgumentException: Cannot subclass final class class org.slf4j.LoggerFactory when mocking the loggerjava.lang.IllegalArgumentException:模拟记录器时无法继承最终类类 org.slf4j.LoggerFactory
【发布时间】:2018-02-19 14:36:26
【问题描述】:

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

我想验证日志。

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。最后我得到了不同的例外。

对于当前的方法,我得到了上述异常。

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

【问题讨论】:

标签: java logging junit mockito


【解决方案1】:

不模拟记录器工厂的解决方案。

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

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");
}

【讨论】:

  • 感谢您的回复。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多