如何将此测试规则转换为Junit5?
我使用Junit4有一些自定义规则,我想将其转换为Junit5。但是,除了我应该使用junit5扩展而不是规则外,我还找不到有关迁移方法实现的良好文档。
public class MyRule implements MethodRule {
private static final Logger LOGGER = LoggerFactory.getLogger(MyRule.class);
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation { }
@Override
public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
Statement result = statement;
if (hasMyAnnotation(frameworkMethod)) {
result = new Statement() {
@Override
public void evaluate() {
LOGGER.info("Skipping test");
}
};
}
}
return result;
}
private static boolean hasMyAnnotation(final Annotatable frameworkMethod) {
return frameworkMethod.getAnnotation(MyAnnotation.class) != null;
}
我的课程使用Junit4 语句
,FrameworkMethod
等,以找出我的方法是否具有注释...然后跳过它。我该如何转换?
I have some custom rule using junit4 which I would like to convert to junit5. However I'm not able to find good documentation on migrating a MethodRule implementation other than that I should be using junit5 extension instead of rule.
public class MyRule implements MethodRule {
private static final Logger LOGGER = LoggerFactory.getLogger(MyRule.class);
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation { }
@Override
public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
Statement result = statement;
if (hasMyAnnotation(frameworkMethod)) {
result = new Statement() {
@Override
public void evaluate() {
LOGGER.info("Skipping test");
}
};
}
}
return result;
}
private static boolean hasMyAnnotation(final Annotatable frameworkMethod) {
return frameworkMethod.getAnnotation(MyAnnotation.class) != null;
}
My class is using junit4 Statement
, FrameworkMethod
etc to find out if my method has an annotation... then to skip it. How can I convert this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案1,使用自定义注释禁用测试
Junit 5提供了一种可以控制是否应进行测试的扩展名。这是通过实现“ nofollow noreferrer”> experutionCondition 接口。
扩展实现:
注册扩展:
输出:
解决方案2,通过Invocation Owterptor 跳过测试
nofollow noreferrer“> invocation interceptor”希望拦截测试电话的扩展。
当前实现将与您以前的
规则
完全相同。扩展实施:
注册扩展:
请注意,您可以根据文档。
Solution 1, Disable test with custom annotation
JUnit 5 provides a type of extension that can control whether or not a test should be run. This is defined by implementing the ExecutionCondition interface.
Extension implementation:
Registring extenssion:
Output:
Solution 2, Skip test via invocation interseptor
InvocationInterceptor iterface defines the API for Extensions that wish to intercept calls to test.
Current implementation will behave exactly like your previous
Rule
.Extension implementation:
Registring extenssion:
Please note, you can perform automatic extension registration according to documentation.