[java] TestClassWithoutTestCases - improve message

This commit is contained in:
Andreas Dangel committed 2022-10-20 15:47:07 +02:00
1 parent 5860f64648
commit dd26af7f93
3 files changed
+21 -5

No files matched your search

@@ -45,12 +45,20 @@ public class TestClassWithoutTestCasesRule extends AbstractJavaRule {
}
}
if (testMethods == 0 && nestedTestClasses == 0) {
addViolation(data, node);
addViolation(data, node, getSimpleClassName(node));
}
}
return data;
}
private String getSimpleClassName(ASTClassOrInterfaceBody node) {
JavaNode parent = node.getParent();
if (parent instanceof ASTClassOrInterfaceDeclaration) {
return ((ASTClassOrInterfaceDeclaration) parent).getSimpleName();
}
return "<anon>";
}
private boolean isTestClassByPattern(ASTClassOrInterfaceBody node) {
Pattern testClassPattern = getProperty(TEST_CLASS_PATTERN);
if (testClassPattern.pattern().isEmpty()) {
@@ -3332,13 +3332,13 @@ public void foo() {
<rule name="TestClassWithoutTestCases"
language="java"
since="3.0"
message="This class name ends with 'Test' but contains no test cases"
message="The class ''{0}'' might be a test class, but it contains no test cases."
class="net.sourceforge.pmd.lang.java.rule.errorprone.TestClassWithoutTestCasesRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#testclasswithouttestcases">
<description>
Test classes end with the suffix "Test" or "TestCase". Having a non-test class with that name is not a good practice,
since most people will assume it is a test case. Test classes have test methods named "testXXX" (JUnit3) or use
annotations (e.g. `@Test`).
Test classes typically end with the suffix "Test", "Tests" or "TestCase". Having a non-test class with that name
is not a good practice, since most people will assume it is a test case. Test classes have test methods
named "testXXX" (JUnit3) or use annotations (e.g. `@Test`).
The suffix can be configured using the property `testClassPattern`. To disable the detection of possible test classes
by name, set this property to an empty string.
@@ -7,6 +7,10 @@
<test-code>
<description>JUnit3: failure case</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<expected-messages>
<message>The class 'FooTest' might be a test class, but it contains no test cases.</message>
</expected-messages>
<code><![CDATA[
import junit.framework.TestCase;
public class FooTest extends TestCase {
@@ -234,6 +238,10 @@ class MyEmptyCase { }
<rule-property name="testClassPattern">my\.pkg\..*Case$</rule-property>
<expected-problems>2</expected-problems>
<expected-linenumbers>2,3</expected-linenumbers>
<expected-messages>
<message>The class 'MyEmptyCase' might be a test class, but it contains no test cases.</message>
<message>The class 'MyNestedCase' might be a test class, but it contains no test cases.</message>
</expected-messages>
<code><![CDATA[
package my.pkg;
class MyEmptyCase {