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)
...
In my organization I use the PMD ignore option in POM file to suppress all warnings that are generated for client stubs (and kept in separate module) that we auto-generate, as those are third party client-stubs we don't tend to touch them thought there are any warnings or violations and I am assuming the same thing for ...
To do this you have to open the Problems View , select one of the PMD problems and use the Quick Fix entry from the context menu. This opens the Quick Fix Dialog where you can click on the button Select All to select all occurences of the PMD problem and finally click Finish to resolve all those PMD problems.
PMD violations are assigned a priority from 1 (most severe) to 5 (least severe) according the the rule's priority. Violations at or less than this priority level are considered failures and will fail the build if failOnViolation=true and the count exceeds maxAllowedViolations .
Conceptually, PMD rules work by matching a “pattern” against the AST of a file. Rules explore the AST and find nodes that satisfy some conditions that are characteristic of the specific thing the rule is trying to flag. Rules then report a violation on these nodes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With