当前尝试使用JUnit 5,并且希望在我的自动化框架中实现,如果存在我的自定义批注,则可以跳过测试。
如果我想这样做:
public class KnownIssueExtension implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
if(context.getRequiredTestMethod().isAnnotationPresent(KnownIssue.class)){
return disabled("HERE DISABLED");
// if(context.getTestMethod().isPresent(element -> findAnnotation(element, KnownIssue.class))){
} else return enabled("enabled");
}
}
我收到此类异常:
org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.talend.qa.iam.utils.KnownIssueExtension]: Illegal state: required test method is not present in the current ExtensionContext
但是另一种方法是:
context.getTestMethod
不检查是否存在注释。
有人可以指出我做错了什么吗? 预先感谢
答案 0 :(得分:1)
我不知道从那以后API是否发生了变化,但是我想在最初测试JUnit 5时做类似的事情。(请参阅here。)
仅对代码转储感到抱歉,但看起来很简单,很可能是不言自明的:
const coeff = 1000 * 60 * 10;
const date = new Date("2019-01-03T13:18:05.641Z");
let floorDate = new Date(Math.floor(date.getTime() / coeff) * coeff);
console.log(date);
console.log(floorDate);
(注意:未经测试,即使编译也是如此。)
编辑:
哦,我只是注意到 skip ,而不是忽略失败。在这种情况下,您注释掉的代码不能正常工作:
public final class KnownIssueExtension implements TestExecutionExceptionHandler {
@Override
public void handleTestExecutionException(final TestExtensionContext context,
final Throwable throwable)
throws Exception {
final Method testMethod = context.getTestMethod().get();
if (!testMethod.isAnnotationPresent(KnownIssue.class)) {
throw throwable;
}
}
}
答案 1 :(得分:0)
工作解决方案:
public class KnownIssueExtension implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
final Optional<Method> methodOptional = context.getTestMethod();
if (methodOptional.isPresent()
&& methodOptional.get().isAnnotationPresent(KnownIssue.class)) {
return disabled("DISABLED");
}
return enabled("ENABLED");
}
}