Java:在 JUnit 中使用记录器断言*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10734456/
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
Java: using a logger in JUnit assert*
提问by shrini1000
What I want to do is something like the following in JUnit:
我想做的是 JUnit 中的以下内容:
assertTrue(logger.error("the condition is not true"), <a boolean condition>);
so the error message gets logged by a logger, where the logger could be e.g. commons or log4j.
所以错误消息会被记录器记录下来,记录器可以是例如 commons 或 log4j。
But Junit asserts don't take a logger param, so is there some way to achieve this, or do I need to try-catch the assert and log the error message in catch block?
但是 Junit 断言不接受记录器参数,所以有什么方法可以实现这一点,还是我需要尝试捕获断言并在 catch 块中记录错误消息?
回答by Matthew Farwell
You can use a JUnit TestRuleTestWatcher. A TestRule
executes code before and after the test method (similar to @Before
and @After
), but you have access to more information, and more importantly, the result of the test. A TestWatcherdefines methods like succeeded()
, failed()
, starting()
and finished()
, which you can implement to get notified of events.
您可以使用 JUnit TestRule TestWatcher。ATestRule
在测试方法之前和之后执行代码(类似于@Before
和@After
),但您可以访问更多信息,更重要的是,可以访问测试结果。一个TestWatcher定义了类似的方法succeeded()
,failed()
,starting()
并且finished()
,它可以实现得到通知的事件。
The following example simply prints out the failed tests with the failed assertions.
下面的示例简单地打印出带有失败断言的失败测试。
public class TestWatcherTest {
@Rule
public TestWatcher testWatcher = new TestWatcher() {
protected void failed(Throwable e, Description description) {
System.out.println("" + description.getDisplayName() + " failed " + e.getMessage());
super.failed(e, description);
}
};
@Test
public void test1() {
Assert.assertEquals("hello world", 3, 4);
}
}
You can obviously do what you like instead of the System.out.println(). This produces as output:
你显然可以做你喜欢做的事,而不是 System.out.println()。这产生作为输出:
test1(uk.co.farwell.junit.TestWatcherTest) failed hello world expected:<3> but was:<4>
Note that a failed assertion is an exception, so you'll have access to the stacktrace etc.
请注意,失败的断言是一个例外,因此您可以访问堆栈跟踪等。
回答by duffymo
I would not alter or extend the JUnit classes.
我不会改变或扩展 JUnit 类。
An arrangement where you try/catch and log errors would be preferable.
您尝试/捕获并记录错误的安排会更可取。
The problem is that failing an Assert is not necessarily an exception.
问题是断言失败不一定是例外。
It feels like you're trying to make log4j into the reporting capability that already exists. I'd advise that you look into the Ant JUnit reporting task - it'll give you a nice looking report that will be more useful than a log.
感觉就像您正在尝试将 log4j 变成已经存在的报告功能。我建议您查看 Ant JUnit 报告任务 - 它会给您一个漂亮的报告,它比日志更有用。
UPDATE:
更新:
You can always add another log4j file appender. Let log4j write messages to both the console and the file that you choose. No changes in your code at all if I'm correct.
您始终可以添加另一个 log4j 文件附加程序。让 log4j 将消息写入控制台和您选择的文件。如果我是对的,你的代码根本没有变化。
回答by Harshavardhan Konakanchi
Better use this then
最好使用这个然后
if(!<condition>) {
logger.error("my message");
Assert.fail();
}