6

I'm being plagued by CheckStyle warnings about missing @throws in the JavaDoc of test methods.

I'm using writing a test method like this:

/**
 * Check that something works. <== CheckStyle wants @throws here
 */
@Test
public void testSomething() throws Exception {
  ...
}

Is there a configurable way to tell CheckStyle to ignore this ?

The "throws" clause is there especially because it is a test method; where typically exception handling is ignored.

2
  • 2
    Is the Javadoc comment necessary in the first place for unit tests? I would be more inclined to get rid of the entire JavaDoc comment and make checkstyle ignore the fact that the method is missing the doc. Commented Jan 26, 2012 at 22:12
  • I agree with Jesse, you should pretty much disable JavaDoc checking for unit tests. Other than that, CheckStyle is right: If your method throws something, you must explain why it happens (remember this does not apply to unit tests). Commented Dec 8, 2015 at 13:40

3 Answers 3

4
+50

A very suitable approach is:
to have one checkStyle rule set for src code and another for test.
In eclipse you can configure that in Project->Properties->Checkstyle.

Checkstyle rules for src are not always suitable for test code, like shown by the OP in the example above. Other candidate rules to disable in test would be The MagicNumber check.

If you have the need to disable one or more checks for the next N lines in src, you should configure: (see also http://checkstyle.sourceforge.net/config.html)

To configure a filter so that CHECKSTYLE IGNORE check FOR NEXT var LINES avoids triggering any audits for the given check for the current line and the next var lines (for a total of var+1 lines):

 <module name="SuppressWithNearbyCommentFilter">
     <property name="commentFormat" value="CHECKSTYLE IGNORE (\w+) FOR NEXT (\d+) LINES"/>
     <property name="checkFormat" value="$1"/>
     <property name="influenceFormat" value="$2"/> </module>

Then in your src or test code you can disable specific Checkstyle rules with

    /**
     * Tests worker1.
     */
    //CHECKSTYLE IGNORE <Rule> 1
    public void testWorker1() throws Exception {
    }

Where "Rule" is the name of the rule, look that up in the link above.
For ignoring line length limitaion to 80 char, for th enext 10 lines the comment would be

//CHECKSTYLE IGNORE Line 100

Your further can concatenate rules :

//CHECKSTYLE IGNORE Line|MethodLength 100

And if you are a very serious developper you fruther could add a comment why you do this:

//CHECKSTYLE IGNORE Catch 1  Last line of defense: need to catch Exception
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I am running the maven checkstyle plugin in automated integration tests (in jenkins) which count the amount of checkstyle violations in the project. It is not possible (AFAIK) to set a different configuration for different parts of the same maven module. What I meant by more detail in the bounty is that the above proposed solution doesn't really do what OP asks for.
I thin it is possible: We use ant, and we have for test a different target, where other checkstyle rules are used. I am sure this works with maven, too.
@Miquel My solution exactly does what the OP is asking for. It is the recommended solution in an professional SW Team. Two checkstyle configs, one for src and one for test. Even eclipse supports that in the GUI.
Alex, thanks for the details. You are right, it does do what OP suggests. It looks like there ought to be a better way with the supression file, but I also don't see a better way than what you propose. Thanks for this solution!
2

Yes. You can specify a suppression filter for checkstyle errors for particular files. See Checkstyle 5.5, section SuppressionFilter. From there,

Filter SuppressionFilter rejects audit events for Check errors according to a suppressions XML document in a file. If there is no configured suppressions file, the Filter accepts all audit events.

<module name="SuppressionFilter">
    <property name="file" value="docs/suppressions.xml"/>
</module>

A suppressions XML document contains a set of suppress elements, where each suppress element can have the following attributes:

  • files - a regular expression matched against the file name associated with an audit event. It is mandatory.
  • checks - a regular expression matched against the name of the check associated with an audit event. Optional if id is specified.
  • id - a string matched against the id of the check associated with an audit event. Optional if checks is specified.
  • lines - a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer. It is optional.
  • columns - a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer. It is optional.

Each audit event is checked against each suppress element. It is suppressed if all specified attributes match against the audit event.

So in your case, you could do something like:

<suppressions>
   <suppress checks="JavadocStyleCheck" files="*Test.java"/>
</suppressions>

I'm not sure if JavadocStyleCheck is really the check you want to remove, but look in the documentation for more.

2 Comments

Thanks ! The syntax for my case would be: <?xml version="1.0"?> <!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "puppycrawl.com/dtds/suppressions_1_1.dtd"> <suppressions> <suppress checks="JavadocMethod" files=".*Test\.java"/> </suppressions> Unfortunately that's a bit more than I would like CheckStyle to skip. Because it will omit the other method javadoc checks too. But I guess this is how far you get w/o programming. :-)
This will prevent all Javadoc checks on all test methods. At the very least, you could go down to supressing only JavadocMethod. Either way, I've set up a bounty to see if this can be done better.
1

You can add this before each method, or just once before the class containing them:

@SuppressWarnings("checkstyle:javadocmethod")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.