Fail the build when naming conventions violated (#3974)
* Failing build if unit test name does not follow one of the following conventions : ..IntegrationTest, ..LongRunningUnitTest, ..ManualTest, ..JdbcTest, ..LiveTest * Custom PMD Rules project * Fixed this issue when unit test classes were not being scanned by custom PMD rule (This happened after I pulled code from upstream project) * Don't fail the build if PMD custom rule fails * Reconfigure PMD * Reformat POM * Adjust PMD rules * Rename test
This commit is contained in:
committed by
GitHub
parent
ecca8c9a89
commit
d337266c7a
@@ -0,0 +1,32 @@
|
||||
package org.baeldung.pmd;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class UnitTestNamingConventionRule extends AbstractJavaRule {
|
||||
|
||||
private static List<String> allowedEndings = Arrays.asList(
|
||||
"IntegrationTest",
|
||||
"LongRunningUnitTest",
|
||||
"ManualTest",
|
||||
"JdbcTest",
|
||||
"LiveTest");
|
||||
|
||||
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
||||
String className = node.getImage();
|
||||
Objects.requireNonNull(className);
|
||||
|
||||
if (className.endsWith("Test") || className.endsWith("Tests")) {
|
||||
if (allowedEndings.stream()
|
||||
.noneMatch(className::endsWith)) {
|
||||
addViolation(data, node);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user