6

I use PMD for a Spring Boot project which contains MockMvc tests. The class enforces the user to catch the general Exception.

class MockMvc {
    public ResultActions perform(RequestBuilder requestBuilder) throws Exception {}
}

The usage leads to a PMD error - SignatureDeclareThrowsException. I would like to suppress the check for all @Test methods. Therefore I tried to follow a Stackoverflow answer but the configuration change has no effect.

<rule ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException" >
    <properties>
        <!-- Ignore @Test methods -->
        <property name="violationSuppressXPath" value="
        //ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='Test']" />
    </properties>
</rule>

How could I achieve it?


Abstract Syntax Tree shows the following sub tree for the test method.

> ClassOrInterfaceBodyDeclaration
  > Annotation
    > MarkerAnnotation
      > Name:Test
  > MethodDeclaration:(public)
    > ResultType:(void)
    ...
sschmeck
  • 7,233
  • 4
  • 40
  • 67
  • 1
    Maybe you could try this way: https://stackoverflow.com/questions/47815987 possible duplicate. Although it will exclude the whole class most likely. – hakamairi Jun 28 '18 at 13:09

2 Answers2

3

The specific problem with test methods can be solved in version with the IgnoreJUnitCompletely property.

<!-- PMD > version 6.* -->
<rule ref="category/java/design.xml/SignatureDeclareThrowsException" >
    <properties>
        <property name="IgnoreJUnitCompletely" value="true" />
    </properties>
</rule>

Before PMD 6 you have to take the rule from typeresolution.xml but not strictexception.xml.

<!-- PMD > version 4.* -->
<rule ref="rulesets/java/typeresolution.xml/SignatureDeclareThrowsException">
    <properties>
        <property name="IgnoreJUnitCompletely" value="true" />
    </properties>
</rule>

But it doesn't answer the question about the violationSuppressXPath problem.

sschmeck
  • 7,233
  • 4
  • 40
  • 67
  • 1
    Cool finding, upvoted. But this feature is there since 4.0, should work in 5.7.0 https://pmd.github.io/pmd-5.7.0/xref/net/sourceforge/pmd/lang/java/typeresolution/rules/SignatureDeclareThrowsException.html unless it needs one of the recent 6.2.0 fixes. – hakamairi Jun 28 '18 at 20:52
  • 1
    @hakamairi Thanks. There are 2 `SignatureDeclareThrowsException` rules, packages `strictexception` and `typeresolution`. Your suggestion works for the second package only. What a mess. ;-) I'll update my answer. – sschmeck Jun 29 '18 at 05:58
1

From the PMD documentation, the JUnit4TestShouldUseTestAnnotation part

//ClassOrInterfaceDeclaration[
   matches(@Image, $testClassPattern)
    or ExtendsList/ClassOrInterfaceType[pmd-java:typeIs('junit.framework.TestCase')]]

/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration[MethodDeclaration[@Public=true()]/MethodDeclarator[starts-with(@Image, 'test')]]
[not(Annotation//Name[pmd-java:typeIs('org.junit.Test')])]

Suggest that Annotation//Name[pmd-java:typeIs('org.junit.Test')] should be enough

hakamairi
  • 4,464
  • 4
  • 30
  • 53