Bjo*_*cke 5 junit interception
当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);
}
}
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用,因为你不能覆盖静态方法.但如果它会这样,这将是很好的,因为每个断言函数都会assertTrue()调用该fail()方法.因此,我可以轻松拦截每一个断言.
在没有实现所有不同风格的情况下,有没有办法做我想做的事情assert...?
您可以编写一个类,该类实现 Assert 中所有方法的完全相同的签名,然后委托给 Assert 方法。
public class MyAssert {
static public void assertTrue(String message, boolean condition) {
Assert.assertTrue(message, condition);
}
...
static public void fail(String message) {
System.err.println("I am intercepting here!");
Assert.fail(message);
}
}
Run Code Online (Sandbox Code Playgroud)
并不完全重新实现所有方法,但仍然很乏味。您的 IDE 可能能够帮助生成委托方法。