简体   繁体   English

java.lang.IllegalArgumentException:嘲笑记录器时无法将最终类类org.slf4j.LoggerFactory子类化

[英]java.lang.IllegalArgumentException: Cannot subclass final class class org.slf4j.LoggerFactory when mocking the logger

I am getting the above exception when i am trying to mock the logger . 尝试嘲笑记录器时,出现上述异常。

I want to verify the logs. 我想验证日志。

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

}

I had spent whole day in to verify the logs in different approaches . 我花了整整一天时间以不同的方式来验证日志。 like using 喜欢使用

TestLogger logger, Mockitto , powermock . TestLogger记录器Mockitto powermock。 Finally i ended up with different exceptions. 最终我以不同的例外告终。

And for the current approach i am getting the above exception . 对于当前的方法,我遇到了上述例外情况。

Suggestions are greatly appreciated .Thanks for the help in advance 建议非常感谢。感谢您的提前帮助

You have a couple of options. 您有两种选择。 One is to follow Darren's advice and capture the logged output and examine it. 一种是遵循Darren的建议并捕获记录的输出并进行检查。 Another is to supply your own LoggerFactory that produces a logger to your liking. 另一个是提供您自己的LoggerFactory来生成您喜欢的记录器。 Note that SFL4J is an API, so that is doable. 请注意,SFL4J是API,因此是可行的。 However, that seems like a lot of work for what you are trying to accomplish. 但是,这似乎是您要完成的工作很多。

The solution without mocking of logger factory. 无需嘲笑记录器工厂的解决方案。

Have a method for lazy loading of the logger: 有一种延迟加载记录器的方法:

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

Your test has an instance of your service class with overridden getLogger() method: 您的测试具有服务类的实例,该实例具有重写的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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:找不到符号符号:类getLogger位置:类org.slf4j.LoggerFactory - java: cannot find symbol symbol: class getLogger location: class org.slf4j.LoggerFactory java.lang.NoClassDefFoundError:无法初始化类org.slf4j.LoggerFactory - java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.LoggerFactory 引起:java.lang.ClassNotFoundException:org.slf4j.LoggerFactory - Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory - java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory NoClassDefFoundError:org.slf4j.LoggerFactory是受限制的类 - NoClassDefFoundError: org.slf4j.LoggerFactory is a restricted class java.lang.IllegalAccessError:试图从类 org.slf4j.LoggerFactory 访问字段 org.slf4j.impl.StaticLoggerBinder.SINGLETON - java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory java.lang.IllegalAccessError: 试图从 SpringBoot 中的 org.slf4j.LoggerFactory 类访问字段 org.slf4j.impl.StaticLoggerBinder.SINGLETON - java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory in SpringBoot java.lang.IllegalAccessError: class org.slf4j.LoggerFactory 试图访问私有字段 org.slf4j.impl.StaticLoggerBinder.ZECF2A6AABCB0538532BA2A7D0D0 - java.lang.IllegalAccessError: class org.slf4j.LoggerFactory tried to access private field org.slf4j.impl.StaticLoggerBinder.SINGLETON JAVA:java.lang.IllegalArgumentException:无法子类化最终类类[Lcom.package.testEntityDO; - JAVA: java.lang.IllegalArgumentException: Cannot subclass final class class [Lcom.package.testEntityDO; DefaultHttpClient引发原因:java.lang.ClassNotFoundException:org.slf4j.LoggerFactory - DefaultHttpClient throws Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM