我的应用程序有几种执行模式,在 1 模式下,我的一些测试会抛出具体异常是正常的。我需要用 @SkipOnFail 之类的东西来注释这个方法,如果抛出异常,它将把方法设置为跳过。
提前致谢!
@Edit(让我的问题更清楚)
@Test(expected=ConcreteException.class)
对我不起作用,因为我需要我的测试通过,即使没有抛出 ConcreteException.class(如果不抛出此异常,junit 中的预期标记会将我的测试标记为失败),否则将被跳过。在所有其他情况下,它应该一如既往地工作。
@对我有用的解决方案(junit v4.7)感谢@axtavt
@Rule
public MethodRule skipRule = new MethodRule() {
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
if(method.getAnnotation(SkipOnFail.class) == null) return base;
return new Statement() {
@Override
public void evaluate() throws Throwable {
try{
base.evaluate();
} catch (ConcreteException e) {
Assume.assumeTrue(false);
}
}
};
}
};
@谢谢
最佳答案
我不认为这样的功能是开箱即用的,但使用 custom TestRule
应该很容易实现和 Assume
,像这样:
@Rule
public TestRule skipRule = new TestRule() {
public Statement apply(final Statement base, Description desc) {
if (desc.getAnnotation(SkipOnFail.class) == null) return base;
return new Statement() {
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (MyExceptoion ex) {
Assume.assumeTrue(false);
}
}
};
}
};
关于java - 如果在执行期间抛出具体异常,如何为 junit 创建自己的注释将跳过测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10804039/