diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java index 9d75de22a3..92bfda43ca 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/AbstractJUnitRule.java @@ -82,11 +82,13 @@ public abstract class AbstractJUnitRule extends AbstractJavaRule { return false; // skip various inapplicable method variations } + boolean result = false; if (isJUnit3Class) { - return isJUnit3Method(method); - } else { - return isJUnit4Method(method); + result = isJUnit3Method(method); } + + result |= isJUnit4Method(method); + return result; } private boolean isJUnit4Method(ASTMethodDeclaration method) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java index aa34cb0982..e93b8f0c9b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/TestClassWithoutTestCasesRule.java @@ -3,7 +3,6 @@ */ package net.sourceforge.pmd.lang.java.rule.junit; -import java.util.Iterator; import java.util.List; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; @@ -21,17 +20,15 @@ public class TestClassWithoutTestCasesRule extends AbstractJUnitRule { boolean testsFound = false; if (m != null) { - for (Iterator<ASTMethodDeclaration> it = m.iterator(); it.hasNext() && !testsFound;) { - ASTMethodDeclaration md = it.next(); - if (!isInInnerClassOrInterface(md) - && isJUnitMethod(md, data)) { - testsFound = true; - } + for (ASTMethodDeclaration md : m) { + if (!isInInnerClassOrInterface(md) && isJUnitMethod(md, data)) { + testsFound = true; + } } } if (!testsFound) { - addViolation(data, node); + addViolation(data, node); } return data; diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml index f714f6f62d..5ff35a6e4f 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/junit/xml/TestClassWithoutTestCases.xml @@ -96,6 +96,23 @@ failure case does not extend TestCase <expected-problems>0</expected-problems> <code><![CDATA[ public class FooTest { +} + ]]></code> + </test-code> + + <test-code> + <description>#1453 False positive when the test class extends an other.</description> + <expected-problems>0</expected-problems> + <code><![CDATA[ +import static org.junit.Assert.*; +import org.junit.Test; + +public class MyTest extends YourTest { + + @Test + public void falseMethod() { + assertFalse(0 == 1); + } } ]]></code> </test-code> diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 7fe6f90e9f..810c8f54a9 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -29,6 +29,8 @@ * [#1449](https://sourceforge.net/p/pmd/bugs/1449/): false positive when casting a variable to short * java-design/AccessorClassGeneration: * [#1452](https://sourceforge.net/p/pmd/bugs/1452/): ArrayIndexOutOfBoundsException with Annotations for AccessorClassGenerationRule +* java-junit/TestClassWithoutTestCases: + * [#1453](https://sourceforge.net/p/pmd/bugs/1453/): Test Class Without Test Cases gives false positive * General * [#1455](https://sourceforge.net/p/pmd/bugs/1455/): PMD doesn't handle Java 8 explicit receiver parameters