In Java, the JUnit runner checks that the @Rule
annotation is applied to a public non-static field or public non-static method which returns either a TestRule or MethodRule.
If there is @Rule
annotation on a field or method, then the value must be a non-null value or you'll get a NullPointerException during execution of the test.
Your example is more complicated than that because Groovy is a dynamic language, so it does its checking at runtime, not compile time. I suspect that collector2 and collector3 aren't actually doing anything. The @Rule
annotation doesn't apply to the field.
collector4 => NullPointerException
collector5 => same as collector5
collector5a => when you execute, I suspect Groovy doesn't find the
expected methods on your somethingThatIsNotRule, or
you're getting a ClassCastException or something similar.
collector5b => same as 5b for anotherRule
For your paradoxes, again, the @Rule
annotation isn't actually applying to the field.
I suspect your confusion comes from the fact that Groovy doesn't complain about the usage of @Rule
on something that isn't a field or method (whereas Java would). It may not complain, but JUnit will ignore such an annotation.