Renamed JUnit4TestShouldUseAfterAnnotation
- call it UnitTest... to be agnostic to the testing framework
This commit is contained in:
parent
4796da0fb2
commit
c5246eca46
@ -18,7 +18,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
|
||||
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.
|
||||
|
||||
* `java/bestpractices/JUnit4TestShouldUseAfterAnnotation` has been renamed to {% rule java/bestpractices/JUnitTestShouldUseAfterAnnotation %}
|
||||
* `java/bestpractices/JUnit4TestShouldUseAfterAnnotation` has been renamed to {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %}
|
||||
|
||||
* `java/bestpractices/JUnit4TestShouldUseBeforeAnnotation` has been renamed to {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %}
|
||||
|
||||
|
@ -666,54 +666,7 @@ public class GoodTest {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="JUnit4TestShouldUseAfterAnnotation" deprecated="true" ref="JUnitTestShouldUseAfterAnnotation" />
|
||||
|
||||
<rule name="JUnitTestShouldUseAfterAnnotation"
|
||||
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"
|
||||
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#junittestshoulduseafterannotation">
|
||||
<description>
|
||||
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.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![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')]]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class MyTest {
|
||||
public void tearDown() {
|
||||
bad();
|
||||
}
|
||||
}
|
||||
public class MyTest2 {
|
||||
@After public void tearDown() {
|
||||
good();
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
<rule name="JUnit4TestShouldUseAfterAnnotation" deprecated="true" ref="UnitTestShouldUseAfterAnnotation" />
|
||||
|
||||
<rule name="JUnit4TestShouldUseBeforeAnnotation" deprecated="true" ref="UnitTestShouldUseBeforeAnnotation"/>
|
||||
|
||||
@ -1415,6 +1368,53 @@ class Foo{
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<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"
|
||||
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.
|
||||
|
||||
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.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![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')]]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class MyTest {
|
||||
public void tearDown() {
|
||||
bad();
|
||||
}
|
||||
}
|
||||
public class MyTest2 {
|
||||
@After public void tearDown() {
|
||||
good();
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="UnitTestShouldUseBeforeAnnotation"
|
||||
language="java"
|
||||
since="4.0"
|
||||
|
@ -9,7 +9,7 @@ import org.junit.Before;
|
||||
|
||||
import net.sourceforge.pmd.test.PmdRuleTst;
|
||||
|
||||
class JUnitTestShouldUseAfterAnnotationTest extends PmdRuleTst {
|
||||
class UnitTestShouldUseAfterAnnotationTest extends PmdRuleTst {
|
||||
// no additional unit tests
|
||||
|
||||
public static class BaseTest {
|
@ -136,7 +136,7 @@ public class Foo {
|
||||
<description>[java] JUnit4TestShouldUseBeforeAnnotation false positive when overriding setUp #1592</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import net.sourceforge.pmd.lang.java.rule.bestpractices.UnitTestShouldUseBeforeAnnotationTest.BaseTest;
|
||||
import net.sourceforge.pmd.lang.java.rule.bestpractices.UnitTestShouldUseAfterAnnotationTest.BaseTest;
|
||||
|
||||
public class AReallyCoolFeatureTest extends BaseTest {
|
||||
@Override
|
Loading…
x
Reference in New Issue
Block a user