Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore CheckStyle warning for missing @throws for test method?

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.

like image 951
Jan Goyvaerts Avatar asked Jan 26 '12 10:01

Jan Goyvaerts


2 Answers

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
like image 73
AlexWien Avatar answered Sep 22 '22 08:09

AlexWien


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.

like image 45
Matthew Farwell Avatar answered Sep 23 '22 08:09

Matthew Farwell