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

瘦欲@ 提交于 2019-12-10 14:29:44

问题


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.


回答1:


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:


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



回答3:


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

@SuppressWarnings("checkstyle:javadocmethod")



回答4:


@SuppressWarnings("checkstyle:javadocmethod") is the annotation used to suppress warnings for getters and setters or any method in java where you don't need the javadoc



来源:https://stackoverflow.com/questions/9016848/how-to-ignore-checkstyle-warning-for-missing-throws-for-test-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!