From 9337e5a7a21428f92d270c9309b141754e8b7f39 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 3 Oct 2024 17:18:04 +0200 Subject: [PATCH] [java] UnitTestShouldUseAfterAnnotation: Consider JUnit 5 and TestNG --- docs/pages/release_notes.md | 1 + .../resources/category/java/bestpractices.xml | 32 ++- .../xml/UnitTestShouldUseAfterAnnotation.xml | 227 +++++++++++------- 3 files changed, 163 insertions(+), 97 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 645aa4f485..d5d1238fcc 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -17,6 +17,7 @@ This is a {{ site.pmd.release_type }} release. ### 🌟 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 diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 6786399cea..cd51508131 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -1295,36 +1295,46 @@ public class MyTestCase { - 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. 3 - - - Contains tearDown + JUnit4 test class contains tearDown 1 + 3 - Contains @After tearDown + JUnit4 test class contains tearDown with different signature is ok + 0 + + + + + JUnit4 test class contains @After tearDown is ok 0 - Renamed tearDown + JUnit4 test class contains renamed tearDown is ok 0 - #1446 False positive with JUnit4TestShouldUseBeforeAnnotation when TestNG is used - 0 - - - - - #940 False positive with JUnit4TestShouldUseAfterAnnotation when JUnit5's 'AfterEach' is used - 0 - - - - - #940 False positive with JUnit4TestShouldUseAfterAnnotation when JUnit5's 'AfterAll' is used - 0 - - - - Contains tearDown, not a junit 4 test + Contains tearDown, not a JUnit 4/5 or TestNG test is ok 0 - - - Contains tearDown with different signature - 0 - @@ -151,4 +91,119 @@ public class AReallyCoolFeatureTest extends BaseTest { } ]]> + + + TestNG test contains tearDown + 1 + 4 + + + + + TestNG test contains tearDown with different signature is ok (#1446) + 0 + + + + + TestNG test contains tearDown with @AfterMethod is ok + 0 + + + + + TestNG test contains tearDown with @AfterClass is ok + 0 + + + + + JUnit 5 test class contains tearDown + 1 + 4 + + + + + JUnit 5 test class contains tearDown with @AfterEach is ok (#940) + 0 + + + + + JUnit 5 test class contains tearDown with @AfterAll is ok (#940) + 0 + +