3

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:

  1. 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.
  2. 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.

Community
  • 1
  • 1
vpiTriumph
  • 3,116
  • 2
  • 27
  • 39
  • For 2/ http://stackoverflow.com/questions/14335558/why-rule-annotated-fields-in-junit-has-to-be-public –  Jan 22 '16 at 18:15
  • For 1/ http://stackoverflow.com/questions/4023185/how-to-disable-a-particular-checkstyle-rule-for-a-particular-line-of-code (I use this one for the close as duplicate vote) –  Jan 22 '16 at 18:16
  • What Checkstyle version do you use? – Michal Kordas Jan 22 '16 at 18:20
  • @MichalKordas maven-checkstyle-plugin:2.15 – vpiTriumph Jan 22 '16 at 18:28
  • @RC while 2 is explicitly answered, in the case of 1, the article provided is quite general. Still not seeing specifically which `@SuppressWarning` will allow for using `@Rule`. – vpiTriumph Jan 22 '16 at 18:29
  • 1
    Question should be reopened. It has solution: just update Plugin to 2.17 and violation will be gone. – Michal Kordas Jan 22 '16 at 18:32
  • @MichalKordas upgrading from 2.15=>2.17 does indeed fix the issue. Do you know what the name of the rule is in 2.15 for the purpose of suppression providing that would be the best of both worlds. I can change the question a bit and reopen. – vpiTriumph Jan 22 '16 at 18:35
  • 1
    the name should be "checkstyle:visibilitymodifier" http://checkstyle.sourceforge.net/config_design.html#VisibilityModifier –  Jan 22 '16 at 18:36
  • @vpiTriumph it's VisibilityModifier check, your problem was just a bug that was recently fixed – Michal Kordas Jan 22 '16 at 18:36
  • @MichalKordas you should add that as an answer (when reopened) –  Jan 22 '16 at 18:37
  • @RC but question is closed and I can't – Michal Kordas Jan 22 '16 at 18:38
  • Using `@SuppressWarnings("checkstyle:visibilitymodifier")` actually doesn't do the job for the assignment or on the class. Upgrading to 2.17 does indeed suppress the warning. Also, I voted to reopen, may need @RC to cast a final to get the question reopened. – vpiTriumph Jan 22 '16 at 18:41
  • I already voted to reopen (NB: you closed it not me..) and you need 5 votes for a reopen. For the suppresswarning, you need to use the checkstyle one and to enable the annotation in the config. –  Jan 22 '16 at 18:56
  • @vpiTriumph is my answer fine for you? – Michal Kordas Jan 26 '16 at 07:07

3 Answers3

3

In Checkstyle 6.5 VisibilityModifier check gained new property ignoreAnnotationCanonicalNames which is by default set to org.junit.Rule, so @Rule fields should not trigger violations in this check.

According to release history page, Checkstyle Maven Plugin 2.15 uses Checkstyle 6.1.1 underneath, so it's enough to update plugin to 2.17 to have the fix.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
2

In your checkstyle xml you can specify the suppression filter as follows:

<module name="SuppressionFilter">
  <property name="file" value="${samedir}/suppressions.xml"/>
</module>

And then you can explicitly exclude that check from tests:

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
        <suppress checks="VisibilityModifier" files="[/\\]src[/\\]test[/\\]java[/\\]" /> 
</suppressions>
Hamel Kothari
  • 717
  • 4
  • 11
0

Another option that can be used to solve this issue without upgrading to version 2.17 of the plugin is to simply use comments to toggle the checkstyle on and off:

//CHECKSTYLE:OFF
@Rule
public ExpectedException thrown = ExpectedException.none();
//CHECKSTYLE:ON
vpiTriumph
  • 3,116
  • 2
  • 27
  • 39