Junit @Rules and maven-checkstyle-plugin seem to be at odds with each other.
If a @Rule
is private
or package-private then the result is:
java.lang.Exception: The @Rule 'someRule' must be public.
at org.junit.internal.runners.rules.RuleFieldValidator.addError
However if @Rule
is made public
the checkstyle will complain:
[INFO] --- maven-checkstyle-plugin:2.15:checkstyle (checkstyle-test) @ web-edge-server ---
[INFO] Starting audit...
SomeTest.java: Variable 'someRule' must be private and have accessor methods.
So two questions:
- If I'd like to continue to use the
@Rule
annotation what is there a way to suppress this warning locally, or within tests, without disabling the checkstyle rule globally. - Does anyone know why junit requires
@Rule
to be public? This seems unnecessary as it could be made accessible using reflection in any case.
Edit: Item 2 is specifically covered by this SO question.