如何将Date参数传递给注释,然后在junit 5扩展中验证它?

问题描述 投票:0回答:1

所以我有扩展根据某些条件跳过测试。其中一个条件应该是日期,例如修复的目标日期,在此之前,我们将跳过测试:

public class KnownIssueExtension implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {

    Date now = new Date();

    final Optional<Method> testMethod = context.getTestMethod();
    if (testMethod.isPresent()
            && testMethod.get().isAnnotationPresent(KnownIssue.class) && now.before(testMethod.get().getAnnotation(KnownIssue.class).date())) {
        return disabled(testMethod.get().getAnnotation(KnownIssue.class).description());
    }
    return enabled("");
}

所以我想将当前日期转换为注释,如参数,但我不能分配任何默认值或类似的东西。

现在它看起来像:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface KnownIssue {
    String description()
            default "Please set the reason for the expected failure like: @KnownIssue(description = \"TPSVC-12345\")";

Date date()
            default "I DONT KNOW WHAT SHOULD BE HERE, I have tried new Date(), but it's not working"

}
java junit interface annotations junit5
1个回答
0
投票

按如下方式实现注释。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface KnownIssue {

    String since();

    String description() default "";
}

记录since属性需要符合ISO标准日期格式YYYY-MM-DD

然后将String解析为java.util.Date(例如,通过java.text.DateFormat.parse(String))或java.time.LocalDate(例如,通过java.time.LocalDate.parse(CharSequence))。

© www.soinside.com 2019 - 2024. All rights reserved.