忽略具有Test注释的方法的PMD规则

ssc*_*eck 5 java junit pmd abstract-syntax-tree

我将PMD用于包含MockMvc测试的Spring Boot项目.该类强制用户捕获常规Exception.

class MockMvc {
    public ResultActions perform(RequestBuilder requestBuilder) throws Exception {}
}
Run Code Online (Sandbox Code Playgroud)

使用会导致PMD错误 - SignatureDeclareThrowsException.我想抑制所有@Test方法的检查.因此,我尝试遵循Stackoverflow的答案,但配置更改无效.

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

我怎么能实现它?


抽象语法树为测试方法显示以下子树.

> ClassOrInterfaceBodyDeclaration
  > Annotation
    > MarkerAnnotation
      > Name:Test
  > MethodDeclaration:(public)
    > ResultType:(void)
    ...
Run Code Online (Sandbox Code Playgroud)

ssc*_*eck 3

测试方法的具体问题可以在具有属性的版本中解决IgnoreJUnitCompletely

<!-- PMD > version 6.* -->
<rule ref="category/java/design.xml/SignatureDeclareThrowsException" >
    <properties>
        <property name="IgnoreJUnitCompletely" value="true" />
    </properties>
</rule>
Run Code Online (Sandbox Code Playgroud)

typeresolution.xml在 PMD 6 之前,您必须采用来自但不是的规则strictexception.xml

<!-- PMD > version 4.* -->
<rule ref="rulesets/java/typeresolution.xml/SignatureDeclareThrowsException">
    <properties>
        <property name="IgnoreJUnitCompletely" value="true" />
    </properties>
</rule>
Run Code Online (Sandbox Code Playgroud)

但它并没有回答有关问题的问题violationSuppressXPath