如何使用 junit 测试 log4j 是否记录了警告? [英] How can I test with junit that a warning was logged with log4j?
问题描述
我正在测试一种方法,该方法在出现问题时记录警告并返回 null.
类似的东西:
I'm testing a method that logs warnings when something went wrong and returns null.
something like:
private static final Logger log = Logger.getLogger(Clazz.class.getName());
....
if (file == null || !file.exists()) {
// if File not found
log.warn("File not found: "+file.toString());
} else if (!file.canWrite()) {
// if file is read only
log.warn("File is read-only: "+file.toString());
} else {
// all checks passed, can return an working file.
return file;
}
return null;
我想用 junit 测试是否发出警告,除了返回 null 外,在所有情况下(例如,找不到文件,文件是只读的).
有什么想法吗?
谢谢,阿萨夫:-)
i'd like to test with junit that a warning was issued, in addition to returning null, in all cases (e.g. file not found, file is read-only).
any ideas?
thanks, asaf :-)
我对 Aaron 的回答的实现(加上 peter 的评论):
My implementation of Aaron's answer (plus peter's remark):
public class UnitTest {
...
@BeforeClass
public static void setUpOnce() {
appenders = new Vector<Appender>(2);
// 1. just a printout appender:
appenders.add(new ConsoleAppender(new PatternLayout("%d [%t] %-5p %c - %m%n")));
// 2. the appender to test against:
writer = new StringWriter();
appenders.add(new WriterAppender(new PatternLayout("%p, %m%n"),writer));
}
@Before
public void setUp() {
// Unit Under Test:
unit = new TestUnit();
// setting test appenders:
for (Appender appender : appenders) {
TestUnit.log.addAppender(appender);
}
// saving additivity and turning it off:
additivity = TestUnit.log.getAdditivity();
TestUnit.log.setAdditivity(false);
}
@After
public void tearDown() {
unit = null;
for (Appender appender : appenders) {
TestUnit.log.removeAppender(appender);
}
TestUnit.log.setAdditivity(additivity);
}
@Test
public void testGetFile() {
// start fresh:
File file;
writer.getBuffer().setLength(0);
// 1. test null file:
System.out.println(" 1. test null file.");
file = unit.getFile(null);
assertNull(file);
assertTrue(writer.toString(), writer.toString().startsWith("WARN, File not found"));
writer.getBuffer().setLength(0);
// 2. test empty file:
System.out.println(" 2. test empty file.");
file = unit.getFile("");
assertNull(file);
assertTrue(writer.toString(), writer.toString().startsWith("WARN, File not found"));
writer.getBuffer().setLength(0);
}
谢谢各位,
推荐答案
在单元测试的设置中:
- 获取相同的记录器
- 使其无添加
添加一个能够记住列表中消息的 appender:
- Get the same logger
- Make it non-additive
Add an appender which remembers the messages in a list:
public class TestAppender extends AppenderSkeleton {
public List<String> messages = new ArrayList<String>();
public void doAppend(LoggingEvent event) {
messages.add( event.getMessage().toString() );
}
}
将 appender 添加到记录器
Add the appender to the logger
现在您可以调用您的代码了.测试后,您将在列表中找到所有日志消息.如果需要,请添加日志级别 (messages.add( event.getLevel() + " " + event.getMessage() );
).
Now you can call your code. After the test, you will find all log messages in the list. Add the log level if you want (messages.add( event.getLevel() + " " + event.getMessage() );
).
在tearDown()
中,再次移除appender并启用可加性.
In tearDown()
, remove the appender again and enable additivity.
这篇关于如何使用 junit 测试 log4j 是否记录了警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!