Add support for JUnit 5 method naming

Tests that are annotated with org.junit.jupiter.api.Test were not
recognized as unit tests.

Provide detection for JUnit 5 style tests and also provide the possibility
to customize the method naming regex similar to the existing ones.
This commit is contained in:
Ritz, Bruno
2020-05-05 20:36:20 +02:00
parent 4d2749b697
commit db590fdefd

View File

@ -35,6 +35,7 @@ public class MethodNamingConventionsRule extends AbstractNamingConventionRule<AS
private final PropertyDescriptor<Pattern> nativeRegex = defaultProp("native").build();
private final PropertyDescriptor<Pattern> junit3Regex = defaultProp("JUnit 3 test").defaultValue("test[A-Z0-9][a-zA-Z0-9]*").build();
private final PropertyDescriptor<Pattern> junit4Regex = defaultProp("JUnit 4 test").build();
private final PropertyDescriptor<Pattern> junit5Regex = defaultProp("JUnit 5 test").build();
public MethodNamingConventionsRule() {
@ -45,6 +46,11 @@ public class MethodNamingConventionsRule extends AbstractNamingConventionRule<AS
definePropertyDescriptor(nativeRegex);
definePropertyDescriptor(junit3Regex);
definePropertyDescriptor(junit4Regex);
definePropertyDescriptor(junit5Regex);
}
private boolean isJunit5Test(ASTMethodDeclaration node) {
return node.isAnnotationPresent("org.junit.jupiter.api.Test");
}
private boolean isJunit4Test(ASTMethodDeclaration node) {
@ -85,6 +91,8 @@ public class MethodNamingConventionsRule extends AbstractNamingConventionRule<AS
}
} else if (node.isStatic()) {
checkMatches(node, staticRegex, data);
} else if (isJunit5Test(node)) {
checkMatches(node, junit5Regex, data);
} else if (isJunit4Test(node)) {
checkMatches(node, junit4Regex, data);
} else if (isJunit3Test(node)) {