[英]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.