简体   繁体   English

JUnit自定义规则仅在带注释的方法上运行

[英]JUnit custom Rule to run only on Annotated methods

I'm trying to create a custom rule in JUnit which only runs on test methods that I have a custom annotation on. 我正在尝试在JUnit中创建一个自定义规则,它只运行在我有自定义注释的测试方法上。 The thing is I can't see a way to find out if the method wrapped in the Statement instance has the annotation set. 问题是我无法找到一种方法来查明Statement实例中包含的方法是否具有注释集。 I was thinking I could use reflection but how could I tell if the method in the Statement represents one of the annotated methods? 我以为我可以使用反射但是如何判断Statement中的方法是否代表一个带注释的方法? Not really sure how to approach this. 不太确定如何处理这个问题。 Thanks. 谢谢。

You can simply use the instance of the Description class passed into the Statement. 您只需使用传递给Statement的Description类的实例即可。 See the javadoc for Description#getAnnotation() . 有关Description#getAnnotation()的信息,请参阅javadoc

For more information, please see my answer to Mark unit test as an expected failure in JUnit . 有关更多信息,请参阅我对Mark单元测试的回答, 作为JUnit中的预期失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM