- fix the JUnit4TestShouldUseTestAnnotation rule for TestNG,
- add test
- update changelog
This commit is contained in:
Krystian Dabrowski
2023-08-04 10:45:05 +02:00
parent 3e2de67190
commit 91d7cc2f21
3 changed files with 21 additions and 3 deletions

View File

@ -49,6 +49,7 @@ The remaining section describes the complete release notes for 7.0.0.
* [#4596](https://github.com/pmd/pmd/issues/4596): \[apex] ExcessivePublicCount ignores properties
* java
* [#4401](https://github.com/pmd/pmd/issues/4401): \[java] PMD 7 fails to build under Java 19
* [#4634](https://github.com/pmd/pmd/issues/4634): \[java] JUnit4TestShouldUseTestAnnotation false positive with TestNG
#### API Changes

View File

@ -758,13 +758,14 @@ public class MyTest2 {
<rule name="JUnit4TestShouldUseTestAnnotation"
language="java"
since="4.0"
message="JUnit 4 tests that execute tests should use the @Test annotation, JUnit 5 tests should use @Test, @RepeatedTest, @TestFactory, @TestTemplate or @ParameterizedTest"
message="Unit tests that execute tests should use the @Test annotation. In case of JUnit 5, test methods might use @RepeatedTest, @TestFactory, @TestTemplate or @ParameterizedTest annotations instead."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#junit4testshouldusetestannotation">
<description>
In JUnit 3, the framework executed all methods which started with the word test as a unit test.
In JUnit 4, only methods annotated with the @Test annotation are executed.
In JUnit 5, one of the following annotations should be used for tests: @Test, @RepeatedTest, @TestFactory, @TestTemplate or @ParameterizedTest.
In TestNG, only methods annotated with the @Test annotation are executed.
</description>
<priority>3</priority>
<properties>
@ -778,9 +779,12 @@ In JUnit 5, one of the following annotations should be used for tests: @Test, @R
and starts-with(@Name, 'test')
and not(ModifierList/Annotation[
pmd-java:typeIs('org.junit.Test')
or pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory') or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.api.Test')
or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory')
or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.params.ParameterizedTest')
or pmd-java:typeIs('org.testng.annotations.Test')
]
)
]

View File

@ -187,6 +187,19 @@ public class MyTests {
@ParameterizedTest
@ValueSource(strings = {"Hello", "World"})
public void testParameterized(final String value) { }
}
]]></code>
</test-code>
<test-code>
<description>#4634 JUnit4TestShouldUseTestAnnotation rule false positive with TestNG</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.Test;
public class Foo {
@Test
public void testFoo() { }
}
]]></code>
</test-code>