From 33c737718c0ff7a02739e8c7c2a1e026d17d0ec0 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 3 Oct 2024 17:01:24 +0200 Subject: [PATCH] [java] UnitTestShouldUseBeforeAnnotation: Consider JUnit 5 and TestNG --- docs/pages/release_notes.md | 3 + .../resources/category/java/bestpractices.xml | 28 ++- .../xml/UnitTestShouldUseBeforeAnnotation.xml | 213 +++++++++++------- 3 files changed, 159 insertions(+), 85 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 0068c0480d..645aa4f485 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -16,6 +16,9 @@ This is a {{ site.pmd.release_type }} release. ### 🌟 Rule Changes +#### Changed Rules +* {% 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. diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 2698fbab73..6786399cea 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -1342,16 +1342,20 @@ public class MyTest2 { - 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. 3 @@ -1363,9 +1367,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') + ]] ]]> diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnitTestShouldUseBeforeAnnotation.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnitTestShouldUseBeforeAnnotation.xml index 10d6fbc524..d9ae22cfd4 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnitTestShouldUseBeforeAnnotation.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnitTestShouldUseBeforeAnnotation.xml @@ -5,8 +5,9 @@ xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> - Contains setUp + JUnit4 Test class contains setUp 1 + 3 - Contains @setUp + JUnit4 Test class with Before is ok 0 - Renamed setup + JUnit4 Test class with renamed setup using Before is ok 0 - #1446 False positive with JUnit4TestShouldUseBeforeAnnotation when TestNG is used + Contains setUp, not a JUnit 4/5/TestNG test 0 + + + + JUnit4 Test class contains setUp with different signature is ok + 0 + - - - - #940 False positive with JUnit4TestShouldUseBeforeAnnotation when JUnit5's 'BeforeEach' is used - 0 - - - - - #940 False positive with JUnit4TestShouldUseBeforeAnnotation when JUnit5's 'BeforeAll' is used - 0 - - - - Contains setUp, not a junit 4 test - 0 - - - - Contains setUp with different signature - 0 - +]]> @@ -173,4 +118,120 @@ public class AReallyCoolFeatureTest extends BaseTest { } ]]> + + + TestNG class contains setUp + 1 + 4 + + + + + TestNG class contains setUp with different signature is ok (#1446) + 0 + + + + + TestNG with @BeforeMethod is ok (#1446) + 0 + + + + + TestNG with @BeforeClass is ok + 0 + + + + + JUnit5 Test class contains setUp + 1 + 4 + + + + + JUnit5 Test class with BeforeEach is ok (#940) + 0 + + + + + JUnit5 Test class with BeforeAll is ok (#940) + 0 + +