[java] Improve UnitTestShouldUse{After,Before}Annotation rules to support JUnit5 and TestNG (#5245)

This commit is contained in:
Andreas Dangel 2024-10-24 10:43:48 +02:00 committed by GitHub
commit 40ed4590be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 325 additions and 184 deletions

View File

@ -23,6 +23,10 @@ See [PR #5040](https://github.com/pmd/pmd/pull/5040) for details.
### 🌟 Rule Changes
#### Changed Rules
* {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) now also considers JUnit 5 and TestNG tests.
* {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) now also considers JUnit 5 and TestNG tests.
#### Renamed Rules
* Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called
after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG.
@ -59,6 +63,7 @@ The old rule names still work but are deprecated.
* [#5040](https://github.com/pmd/pmd/pull/5040): \[cpp] Ignore literals and ignore identifiers capability to C++ CPD - [Jakub Dupak](https://github.com/jdupak) (@jdupak)
* [#5225](https://github.com/pmd/pmd/pull/5225): Fix #5067: \[java] CloseResource: False positive for FileSystems.getDefault() - [Lukas Gräf](https://github.com/lukasgraef) (@lukasgraef)
* [#5241](https://github.com/pmd/pmd/pull/5241): Ignore javacc code in coverage report - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
* [#5245](https://github.com/pmd/pmd/pull/5245): \[java] Improve UnitTestShouldUse{After,Before}Annotation rules to support JUnit5 and TestNG - [Andreas Dangel](https://github.com/adangel) (@adangel)
* [#5258](https://github.com/pmd/pmd/pull/5258): Ignore generated antlr classes in coverage reports - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
* [#5264](https://github.com/pmd/pmd/pull/5264): Fix #5261: \[java] Fix NPE with empty pattern list - [Clément Fournier](https://github.com/oowekyala) (@oowekyala)
* [#5269](https://github.com/pmd/pmd/pull/5269): Fix #5253: \[java] Support Boolean wrapper class for BooleanGetMethodName rule - [Aryant Tripathi](https://github.com/Aryant-Tripathi) (@Aryant-Tripathi)

View File

@ -1329,38 +1329,48 @@ public class Foo {
<rule name="UnitTestShouldUseAfterAnnotation"
language="java"
since="4.0"
message="JUnit 4 tests that clean up tests should use the @After annotation, JUnit5 tests should use @AfterEach or @AfterAll"
message="Apply the correct annotation if this method is used to clean up the tests"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unittestshoulduseafterannotation">
<description>
This rule detects methods called `tearDown()` that are not properly annotated as a cleanup method.
This is primarily intended to assist in upgrading from JUnit 3, where tear down methods were required to be called `tearDown()`.
To a lesser extent, this may help detect omissions under newer JUnit versions, as long as you are following this convention to name the methods.
This rule detects methods called `tearDown()` that are not properly annotated as a cleanup method.
This is primarily intended to assist in upgrading from JUnit 3, where tear down methods were required to be called `tearDown()`.
To a lesser extent, this may help detect omissions even under newer JUnit versions or under TestNG,
as long as you are following this convention to name the methods.
JUnit 4 will only execute methods annotated with `@After` after running each test.
JUnit 5 introduced `@AfterEach` and `@AfterAll` annotations to execute methods after each test or after all tests in the class, respectively.
* JUnit 4 will only execute methods annotated with `@After` after running each test.
* JUnit 5 introduced `@AfterEach` and `@AfterAll` annotations to execute methods after each test or after
all tests in the class, respectively.
* TestNG provides the annotations `@AfterMethod` and `@AfterClass` to execute methods after each test or after
tests in the class, respectively.
Note: This rule was named JUnit4TestShouldUseAfterAnnotation before PMD 7.7.0.
Note: This rule was named JUnit4TestShouldUseAfterAnnotation before PMD 7.7.0.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
<![CDATA[
//MethodDeclaration[@Name='tearDown' and @Arity=0]
[not(ModifierList/Annotation[
pmd-java:typeIs('org.junit.After')
or pmd-java:typeIs('org.junit.jupiter.api.AfterEach')
or pmd-java:typeIs('org.junit.jupiter.api.AfterAll')
or pmd-java:typeIs('org.testng.annotations.AfterMethod')])]
(: Make sure this is a junit 4 class :)
[../MethodDeclaration[pmd-java:hasAnnotation('org.junit.Test')]]
or pmd-java:typeIs('org.testng.annotations.AfterClass')
or pmd-java:typeIs('org.testng.annotations.AfterMethod')
])]
(: Make sure this is a JUnit 4/5 or TestNG class :)
[../MethodDeclaration[
pmd-java:hasAnnotation('org.junit.Test')
or pmd-java:hasAnnotation('org.junit.jupiter.api.Test')
or pmd-java:hasAnnotation('org.testng.annotations.Test')
]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
<![CDATA[
public class MyTest {
public void tearDown() {
bad();
@ -1378,18 +1388,22 @@ public class MyTest2 {
<rule name="UnitTestShouldUseBeforeAnnotation"
language="java"
since="4.0"
message="JUnit 4 tests that set up tests should use the @Before annotation, JUnit5 tests should use @BeforeEach or @BeforeAll"
message="Apply the correct annotation if this method is used to set up the tests"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unittestshouldusebeforeannotation">
<description>
This rule detects methods called `setUp()` that are not properly annotated as a setup method.
This is primarily intended to assist in upgrading from JUnit 3, where setup methods were required to be called `setUp()`.
To a lesser extent, this may help detect omissions even under newer JUnit versions, as long as you are following this convention to name the methods.
This rule detects methods called `setUp()` that are not properly annotated as a setup method.
This is primarily intended to assist in upgrading from JUnit 3, where setup methods were required to be called `setUp()`.
To a lesser extent, this may help detect omissions even under newer JUnit versions or under TestNG,
as long as you are following this convention to name the methods.
JUnit 4 will only execute methods annotated with `@Before` before all tests.
JUnit 5 introduced `@BeforeEach` and `@BeforeAll` annotations to execute methods before each test or before all tests in the class, respectively.
* JUnit 4 will only execute methods annotated with `@Before` before all tests.
* JUnit 5 introduced `@BeforeEach` and `@BeforeAll` annotations to execute methods before each test or before all
tests in the class, respectively.
* TestNG provides the annotations `@BeforeMethod` and `@BeforeClass` to execute methods before each test or before
tests in the class, respectively.
Note: This rule was named JUnit4TestShouldUseBeforeAnnotation before PMD 7.7.0.
Note: This rule was named JUnit4TestShouldUseBeforeAnnotation before PMD 7.7.0.
</description>
<priority>3</priority>
<properties>
@ -1401,9 +1415,15 @@ public class MyTest2 {
pmd-java:typeIs('org.junit.Before')
or pmd-java:typeIs('org.junit.jupiter.api.BeforeEach')
or pmd-java:typeIs('org.junit.jupiter.api.BeforeAll')
or pmd-java:typeIs('org.testng.annotations.BeforeMethod')])]
(: Make sure this is a junit 4 class :)
[../MethodDeclaration[pmd-java:hasAnnotation('org.junit.Test')]]
or pmd-java:typeIs('org.testng.annotations.BeforeMethod')
or pmd-java:typeIs('org.testng.annotations.BeforeClass')
])]
(: Make sure this is a JUnit 4/5 or TestNG class :)
[../MethodDeclaration[
pmd-java:hasAnnotation('org.junit.Test')
or pmd-java:hasAnnotation('org.junit.jupiter.api.Test')
or pmd-java:hasAnnotation('org.testng.annotations.Test')
]]
]]>
</value>
</property>

View File

@ -5,129 +5,69 @@
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Contains tearDown</description>
<description>JUnit4 test class contains tearDown</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
import org.junit.Test;
public class Foo {
public void tearDown() {
}
public void tearDown() {}
@Test
public void foo() {
}
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>Contains @After tearDown</description>
<description>JUnit4 test class contains tearDown with different signature is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Test;
public class Foo {
public void tearDown(int something) {}
@Test
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit4 test class contains @After tearDown is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Test;
import org.junit.After;
public class Foo {
@After
public void tearDown() {
}
public void tearDown() {}
@Test
public void foo() {
}
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>Renamed tearDown</description>
<description>JUnit4 test class contains renamed tearDown is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Test;
import org.junit.After;
public class Foo {
@After
public void clean() {
}
public void clean() {}
@Test
public void foo() {
}
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>#1446 False positive with JUnit4TestShouldUseBeforeAnnotation when TestNG is used</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class Foo {
@AfterMethod
public void tearDown(java.lang.reflect.Method m) {
//...
}
@Test
public void someTest() {}
}
]]></code>
</test-code>
<test-code>
<description>#940 False positive with JUnit4TestShouldUseAfterAnnotation when JUnit5's 'AfterEach' is used</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.*;
public class Foo {
@AfterEach
public void tearDown() {
//...
}
@Test
public void someTest() {}
}
]]></code>
</test-code>
<test-code>
<description>#940 False positive with JUnit4TestShouldUseAfterAnnotation when JUnit5's 'AfterAll' is used</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.*;
public class Foo {
@AfterAll
public void tearDown() {
//...
}
@Test
public void someTest() {}
}
]]></code>
</test-code>
<test-code>
<description>Contains tearDown, not a junit 4 test</description>
<description>Contains tearDown, not a JUnit 4/5 or TestNG test is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void tearDown() {
}
public void foo() {
}
}
]]></code>
</test-code>
<test-code>
<description>Contains tearDown with different signature</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Test;
public class Foo {
public void tearDown(int something) {
}
@Test
public void foo() {
}
public void tearDown() {}
public void foo() {}
}
]]></code>
</test-code>
@ -151,4 +91,119 @@ public class AReallyCoolFeatureTest extends BaseTest {
}
]]></code>
</test-code>
<test-code>
<description>TestNG test contains tearDown</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
import org.testng.annotations.Test;
public class MyTestCase {
public void tearDown() {} // violation expected
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>TestNG test contains tearDown with different signature is ok (#1446)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class Foo {
@AfterMethod
public void tearDown(java.lang.reflect.Method m) {}
@Test
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>TestNG test contains tearDown with @AfterMethod is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class MyTestCase {
@AfterMethod
public void tearDown() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>TestNG test contains tearDown with @AfterClass is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
public class MyTestCase {
@AfterClass
public void tearDown() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit 5 test class contains tearDown</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
import org.junit.jupiter.api.Test;
public class MyTestCase {
public void tearDown() {} // violation expected
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit 5 test class contains tearDown with @AfterEach is ok (#940)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
public class MyTestCase {
@AfterEach
public void tearDown() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit 5 test class contains tearDown with @AfterAll is ok (#940)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
public class MyTestCase {
@AfterAll
public void tearDown() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
</test-data>

View File

@ -5,8 +5,9 @@
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Contains setUp</description>
<description>JUnit4 Test class contains setUp</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
import org.junit.Test;
public class Foo {
@ -20,7 +21,7 @@ public class Foo {
</test-code>
<test-code>
<description>Contains @setUp</description>
<description>JUnit4 Test class with Before is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Before;
@ -37,7 +38,7 @@ public class Foo {
</test-code>
<test-code>
<description>Renamed setup</description>
<description>JUnit4 Test class with renamed setup using Before is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Before;
@ -73,85 +74,29 @@ public class Foo {
</test-code>
<test-code>
<description>#1446 False positive with JUnit4TestShouldUseBeforeAnnotation when TestNG is used</description>
<description>Contains setUp, not a JUnit 4/5/TestNG test</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
public class Foo {
public void setUp() {}
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit4 Test class contains setUp with different signature is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Test;
public class Foo {
@BeforeMethod
public void setUp(Method m) {
//...
}
public void setUp(int something) {}
@Test
public void someTest() {}
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>#940 False positive with JUnit4TestShouldUseBeforeAnnotation when JUnit5's 'BeforeEach' is used</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.*;
public class Foo {
@BeforeEach
public void setUp() {
//...
}
@Test
public void someTest() {}
}
]]></code>
</test-code>
<test-code>
<description>#940 False positive with JUnit4TestShouldUseBeforeAnnotation when JUnit5's 'BeforeAll' is used</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.*;
public class Foo {
@BeforeAll
public void setUp() {
//...
}
@Test
public void someTest() {}
}
]]></code>
</test-code>
<test-code>
<description>Contains setUp, not a junit 4 test</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void setUp() {
}
public void foo() {
}
}
]]></code>
</test-code>
<test-code>
<description>Contains setUp with different signature</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.Test;
public class Foo {
public void setUp(int something) {
}
@Test
public void foo() {
}
}
]]></code>
]]></code>
</test-code>
<test-code>
@ -173,4 +118,120 @@ public class AReallyCoolFeatureTest extends BaseTest {
}
]]></code>
</test-code>
<test-code>
<description>TestNG class contains setUp</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
import org.testng.annotations.Test;
public class MyTestCase {
public void setUp() {} // violation expected here
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>TestNG class contains setUp with different signature is ok (#1446)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.Test;
import java.lang.reflect.Method;
public class Foo {
public void setUp(Method m) {}
@Test
public void foo() {}
}
]]></code>
</test-code>
<test-code>
<description>TestNG with @BeforeMethod is ok (#1446)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class MyTestCase {
@BeforeMethod
public void setUp() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>TestNG with @BeforeClass is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class MyTestCase {
@BeforeClass
public void setUp() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit5 Test class contains setUp</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
import org.junit.jupiter.api.Test;
public class MyTestCase {
public void setUp() {} // violation expected
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit5 Test class with BeforeEach is ok (#940)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MyTestCase {
@BeforeEach
public void setUp() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
<test-code>
<description>JUnit5 Test class with BeforeAll is ok (#940)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class MyTestCase {
@BeforeAll
public void setUp() {}
@Test
public void myTest() {}
}
]]></code>
</test-code>
</test-data>