java – 忽略带有Test注释的方法的PMD规则

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

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

使用导致PMD错误 – SignatureDeclareThrowsException.我想取消所有@Test方法的检查.因此我尝试遵循* answer但配置更改无效.

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

我怎么能实现它?

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

> ClassOrInterfaceBodyDeclaration
  > Annotation
    > MarkerAnnotation
      > Name:Test
  > MethodDeclaration:(public)
    > ResultType:(void)
    ...

解决方法:

可以使用IgnoreJUnitCompletely属性在版本中解决测试方法的特定问题.

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

在PMD 6之前,您必须从typeresolution.xml获取规则,但不能从strictexception.xml获取规则.

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

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

上一篇:JavaWeb(五)Servlet


下一篇:《Linux源码情景分析》--2.1 Linux内存管理的基本框架