java Junit 测试日志语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38861416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Junit testing log statements
提问by Richard
I have a java 8 app with a class foo like this:
我有一个类 foo 的 java 8 应用程序,如下所示:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
private final Logger log = LoggerFactory.getLogger(Foo.class);
public String something() {
log.info("doing foo test");
return "test";
}
}
Which I am writing a JUnit (Junit 4.11) test case for like this:
我正在编写一个 JUnit(Junit 4.11)测试用例,如下所示:
public class FooTest {
private Foo foo;
@Before
public void setUp() throws Exception {
foo = new Foo();
}
@Test
public void testSomething() {
String result = foo.something();
assertEquals(result,"test");
}
}
My objective is to write a test case that tests the something
method for it's return value ANDthe logging statement to make sure something is being logged. I've spent hours scouring the web to figure out how to setup the junit to test the logging statement. I've tried this, this, thisand thismethod to no avail.
我的目标是编写一个测试用例来测试something
方法的返回值和日志记录语句,以确保记录了某些内容。我花了好几个小时在网上搜索以弄清楚如何设置 junit 来测试日志记录语句。我试过这个,这个,这个和这个方法都无济于事。
I don't know if it's something I'm doing wrong or what. But here's my code based on the last example:
我不知道是我做错了什么还是什么。但这是我基于最后一个示例的代码:
public class FooTest {
private Foo foo;
@Mock private Appender appender;
@Captor private ArgumentCaptor captor;
@Before
public void setUp() throws Exception {
foo = new Foo();
MockitoAnnotations.initMocks(this);
Logger.getRootLogger().addAppender(appender);
}
@After
public void tearDown() throws Exception {
Logger.getRootLogger().removeAllAppenders();
}
@Test
public void testSomething() {
String result = foo.something();
assertEquals(result,"test");
verify(appender).doAppend(((LoggingEvent) captor.capture()));
LoggingEvent loggingEvent = (LoggingEvent) captor.getValue();
assertEquals(loggingEvent.getRenderedMessage(), "doing foo test");
}
}
But when I run this I get the following error:
但是当我运行它时,我收到以下错误:
Wanted but not invoked:
appender.doAppend(<Capturing argument>);
-> at <package>.testSomething(FooTest.java:22)
Actually, there were zero interactions with this mock.
Is there an easier way to accomplish what I want? If this is the best way then what am I doing wrong?
有没有更简单的方法来完成我想要的?如果这是最好的方法,那么我做错了什么?
回答by Tassos Bassoukos
Sure!
当然!
- Create your own custom in-memory Singleton
Appender
class - Modify the testing logger configuration to log to this appender in addition to all other logging you will do
clear()
it during each test@Begin
- assert-test it normally before the test finishes.
- 创建您自己的自定义内存中单例
Appender
类 - 除了您将执行的所有其他日志记录之外,修改测试记录器配置以记录到此附加程序
clear()
它在每次测试期间@Begin
- 在测试完成之前,通常对其进行断言测试。
In fact, SLF4J already has an implementationof this which you can look at.
事实上,SLF4J 已经有一个你可以查看的实现。