-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature idea: Annotation Rules #1074
Comments
Note that in this particular example, you could have If you prefer not to have a field, you can apply |
Right, that's how my rule currently works - looking at the I just want to avoid the hassle of having the Also, it would reduce the necessary code within the rule leaving the above mentioned annotation-check out. |
Whether you use an annotation or a field, it still takes up one or two lines in the class (depending on your coding style), so personally I've never quite understood the concern about bloat. YMMV of course :-) If we do decide to allow annotations to declare rules, we need to decide on how to determine what order to apply the rules (see lengthy discussion about this in #766). The ordering problem has been the big blocker for other proposals similar to this one. You do have a good point about the possibility that the rule implementation could be simpler, but that's only the case if the definition of the annotation on the test method is something you control. If you wanted to apply a third-party annotation to a method and have a rule add behavior to methods with that annotation, you'll need to look at the |
The purpose of this ticket is not to allow annotations to declare rules. It is also not about annotations vs. fields. It is about selectively activating a rule for some (not all) test-methods within a class. I've hoped to make that clear in the example provided in the first post. You need the rule-declaration (field/method as of now) and the annotation or a line of code activating the rule within the method. In my opinion, if it is not needed the rule-declaration can be made obsolete with the proposal above. I've looked at #766 where you have provided a very similar solution (the I don't understand your point about using third-party annotations on test-methods. Why would I use a third-party annotation for my own rule and not just create my own? |
I understand what you were originally proposing. If we allow users to apply rules to classes (or methods) by adding a custom annotation, then we need to decide how to handle the case where there are multiple annotations (the ordering of annotations returned by the reflection APIs is undefined). We will also have to decide what to do if there are annotations on the base class, or on the interfaces that the class implements. |
Yes I've read about that problem in #766, and don't have any idea how that could be solved with a clean API. For my use cases however, it's not an issue, as almost all of those rule are independent of each other. A case could be made that if you need to control rule ordering, you have to use the old-fashioned way declaring them in a I'll prolly just end up implementing an own generic |
Just implemented this, works very nicely. Maybe we'll release it as a JUnit extension sometime in the future. Of course it would be a lot better without requiring the If anyone is interested, I've made a small project with the code. |
Based on the documentation for Rules , I do not find a way to define a rule that can be used explicitly at a function level via annotation. How do I do a +1 for this issue ( I am new to github issues) |
@BenRomberg |
@Tibor17 thanks for the feedback. It looks like the JUnit Lambda project is going into this direction, at least that's how I understand the Prototype documentation. Let's wait and see what they come up with :-) |
I've recently run into the problem that I wanted to use an annotation on certain test-methods, but didn't require the explicit rule as a field.
Considering an example, let's say there's a
DatabaseUnavailable
annotation and a correspondingDatabaseUnavailableRule
setting up a defect database connection in case the annotation is present.The rule is completely unnecessary if the test doesn't need to reference the rule's field. We could instead let the annotation reference the rule through a class reference in the annotation. This would change our example to:
With the
DatabaseUnavailable
annotation referencingDatabaseUnavailableRule
within a meta annotation, e.g. looking like:The only limitation I can think of is that the
DatabaseUnavailableRule
would have to have a constructor with no parameters so we can instantiate it with reflection. It would only be instantiated for tests annotated with the annotation.What do you think? I'd be happy to submit a pull-request if it sounds viable.
Could also think of this working as a class annotation for all test-methods.
The text was updated successfully, but these errors were encountered: