忽略带有 Test 注释的方法的 PMD 规则
Ignore PMD rule for methods with a Test annotation
我将 PMD 用于包含 MockMvc
测试的 Spring 引导项目。 class 强制用户捕捉一般 Exception
。
class MockMvc {
public ResultActions perform(RequestBuilder requestBuilder) throws Exception {}
}
该用法导致 PMD 错误 - SignatureDeclareThrowsException. I would like to suppress the check for all @Test
methods. Therefore I tried to follow a 但配置更改无效。
<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)
...
来自PMD documentation,JUnit4TestShouldUseTestAnnotation部分
//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')])]
建议Annotation//Name[pmd-java:typeIs('org.junit.Test')]
应该够了
测试方法的具体问题可以在 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
问题的问题。
我将 PMD 用于包含 MockMvc
测试的 Spring 引导项目。 class 强制用户捕捉一般 Exception
。
class MockMvc {
public ResultActions perform(RequestBuilder requestBuilder) throws Exception {}
}
该用法导致 PMD 错误 - SignatureDeclareThrowsException. I would like to suppress the check for all @Test
methods. Therefore I tried to follow a
<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)
...
来自PMD documentation,JUnit4TestShouldUseTestAnnotation部分
//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')])]
建议Annotation//Name[pmd-java:typeIs('org.junit.Test')]
应该够了
测试方法的具体问题可以在 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
问题的问题。