简体   繁体   中英

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. The thing is I can't see a way to find out if the method wrapped in the Statement instance has the annotation set. I was thinking I could use reflection but how could I tell if the method in the Statement represents one of the annotated methods? Not really sure how to approach this. Thanks.

You can simply use the instance of the Description class passed into the Statement. See the javadoc for Description#getAnnotation() .

For more information, please see my answer to Mark unit test as an expected failure in JUnit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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