From 691731cdd40f05585dc43a8703b38aad70618793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 17 Apr 2024 16:01:09 -0300 Subject: [PATCH] Rename JUnit4TestShouldUseAfterAnnotation - Call it JUnitTestShouldUseAfterAnnotation instead as it not only applies to JUnit4 - Improve the doc to further clarify it's usages --- .../main/resources/category/java/bestpractices.xml | 13 +++++++++---- ...a => JUnitTestShouldUseAfterAnnotationTest.java} | 2 +- ...on.xml => JUnitTestShouldUseAfterAnnotation.xml} | 0 3 files changed, 10 insertions(+), 5 deletions(-) rename pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/{JUnit4TestShouldUseAfterAnnotationTest.java => JUnitTestShouldUseAfterAnnotationTest.java} (88%) rename pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/{JUnit4TestShouldUseAfterAnnotation.xml => JUnitTestShouldUseAfterAnnotation.xml} (100%) diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 20339ec32e..150725b5cb 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -666,15 +666,20 @@ public class GoodTest { - + + + externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#junittestshoulduseafterannotation"> -In JUnit 3, the tearDown method was used to clean up all data entities required in running tests. -JUnit 4 skips the tearDown method and executes all methods annotated with @After after running each test. +This rule detects methods called tearDown() that are not properly annotated as a setup 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. + +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. 3 diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnit4TestShouldUseAfterAnnotationTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitTestShouldUseAfterAnnotationTest.java similarity index 88% rename from pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnit4TestShouldUseAfterAnnotationTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitTestShouldUseAfterAnnotationTest.java index 2ea2b290d2..272d7d9d88 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnit4TestShouldUseAfterAnnotationTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitTestShouldUseAfterAnnotationTest.java @@ -9,7 +9,7 @@ import org.junit.Before; import net.sourceforge.pmd.test.PmdRuleTst; -class JUnit4TestShouldUseAfterAnnotationTest extends PmdRuleTst { +class JUnitTestShouldUseAfterAnnotationTest extends PmdRuleTst { // no additional unit tests public static class BaseTest { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnit4TestShouldUseAfterAnnotation.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnitTestShouldUseAfterAnnotation.xml similarity index 100% rename from pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnit4TestShouldUseAfterAnnotation.xml rename to pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnitTestShouldUseAfterAnnotation.xml