拦截 JUnit Assert 函数
当 JUnit 中的断言失败时,我想做一些“自己的事情”。我想要这样:
public class MyAssert extends org.junit.Assert {
// @Override
static public void fail(String message) {
System.err.println("I am intercepting here!");
org.junit.Assert.fail(message);
}
}
当然,这是行不通的,因为你不能重写静态方法。但如果可以的话,那就太好了,因为像 assertTrue()
这样的每个断言函数都会调用 fail()
方法。因此,我可以轻松拦截每一个断言。
有没有什么方法可以做我想做的事情,而不需要实现所有不同风格的 assert...
?
I would like to do some "own stuff" when an assertion in JUnit fails. I would like to have this:
public class MyAssert extends org.junit.Assert {
// @Override
static public void fail(String message) {
System.err.println("I am intercepting here!");
org.junit.Assert.fail(message);
}
}
Of course, this does not work, because you cannot override static methods. But if it would, this would be nice, because every assert function like assertTrue()
calls the fail()
method. Thus, I could easily intercept every assertion.
Does there exist any way to do what I want to do here, without implementing all different flavors of assert...
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该查看 Junit 4.7 中引入的规则。特别是 TestWatchman:
TestWatchman 是记录测试操作而不对其进行修改的规则的基类。例如,此类将保留每个通过和失败测试的日志:
http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatchman.html
它允许您定义一个可以处理失败测试的 MethodRule (复制自javadoc):
根据评论,TestWatchman 在较新版本的 Junit 中已被废弃,并被 TestWatcher 取代(但功能相同):
http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatcher.html
You should take a look at the Rules, that were introduced in Junit 4.7. Especially TestWatchman:
TestWatchman is a base class for Rules that take note of the testing action, without modifying it. For example, this class will keep a log of each passing and failing test:
http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatchman.html
It lets you define a MethodRule that can handle failing Tests (Copy from the javadoc):
As per comment TestWatchman is depecreated in newer Versions of Junit and replaced with TestWatcher (but the functionality is the same):
http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatcher.html
您可以编写一个类,该类实现 Assert 中所有方法的完全相同的签名,然后委托给 Assert 方法。
并不完全重新实现所有方法,但仍然很乏味。您的 IDE 可能能够帮助生成委托方法。
You could write a class that implements exactly the same signatures of all the methods in Assert and then delegates to the Assert methods.
Not exactly re-implementing all the methods, but tedious nonetheless. Your IDE may be able to help in generating the delegate methods.
对于单独的断言,您可以捕获 AssertionError - 以下内容对我有用。当您只是偶尔需要该功能时,它很有用。
For indivdual assertions you can catch the AssertionError - the following works for me. It's useful when you only need the functionality occasionally.
TestWatchman 被 TestWatcher 替换,它在测试方法级别而不是断言级别进行拦截,并且 Assert 类静态方法不能被重写,因此另一种方法是创建自己的断言方法来断言对象,该方法应该适用于大多数断言
TestWatchman is replaced with TestWatcher and it intercepts at test method level not Assert level and the Assert class static methods cannot be overridden so an alternative is to create your own assert method to assert objects which should work for most of the asserts