Skip to content

Commit

Permalink
#1130 - added JUnit5TestShouldBePackagePrivate to qulice pmd ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
Graur committed Aug 22, 2022
1 parent a27fc28 commit 3f69fe7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
Expand Up @@ -31,19 +31,19 @@

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Simple test of the Main class.
* @since 1.0
*/
public final class MainTest {
final class MainTest {

/**
* Simple testing.
*/
@Test
public void testSquare() {
void testSquare() {
MatcherAssert.assertThat(1, Matchers.is(Main.square(1)));
MatcherAssert.assertThat(4, Matchers.is(Main.square(2)));
}
Expand Down
37 changes: 37 additions & 0 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Expand Up @@ -218,4 +218,41 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
</property>
</properties>
</rule>
<rule name="JUnit5TestShouldBePackagePrivate" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" message="JUnit 5 tests should be package-private">
<description>
Reports JUnit 5 test classes and methods that are not package-private.
Contrary to JUnit 4 tests, which required public visibility to be run by the engine,
JUnit 5 tests can also be run if they’re package-private.
Marking them as such is a good practice to limit their visibility.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<!--Solve priority confict-->
<property name="xpath">
<value><![CDATA[
//ClassOrInterfaceDeclaration[
(: a Junit 5 test class, ie, it has methods with the annotation :)
@Interface=false() and
ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
[Annotation//Name[
pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory') or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.params.ParameterizedTest')
]]
[MethodDeclaration]
]/(
self::*[@Abstract=false() and (@Public=true() or @Protected=true())]
| ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
[Annotation//Name[
pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory') or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.params.ParameterizedTest')
]]
/MethodDeclaration[@Public=true() or @Protected=true()]
)
]]></value>
</property>
</properties>
</rule>
</ruleset>
14 changes: 14 additions & 0 deletions qulice-pmd/src/test/java/com/qulice/pmd/PmdValidatorTest.java
Expand Up @@ -613,4 +613,18 @@ public void allowsDuplicateLiteralsInAnnotations() throws Exception {
)
).validate();
}

/**
* PmdValidator can allow only package private methods,
* annotated @Test, @RepeatedTest, @TestFactory, @TestTemplate or @ParameterizedTest.
* @throws Exception If something wrong happens inside.
*/
@Test
public void testShouldBePackagePrivate() throws Exception {
new PmdAssert(
"TestShouldBePackagePrivate.java",
Matchers.is(false),
Matchers.containsString("JUnit5TestShouldBePackagePrivate")
).validate();
}
}
Expand Up @@ -4,10 +4,10 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AllowAssertFail {
class AllowAssertFail {

@Test
public void prohibitPlainJunitAssertionsInTests() throws Exception {
void prohibitPlainJunitAssertionsInTests() throws Exception {
Matchers.assertThat("errorMessage", "expected", Matchers.is("actual"));
Assertions.fail("fail test");
}
Expand Down
Expand Up @@ -2,20 +2,20 @@

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Collections;

public class SomeTest {
class SomeTest {

@BeforeClass
public static void beforeClass(){
// setup before class
}

@Test
public void emptyTest(){
void emptyTest(){
//test something
}

Expand Down
@@ -0,0 +1,16 @@
package com.qulice.pmd;

import org.junit.jupiter.api.Test;

public class SomeTest {

@Test
public void testOne() {
// empty body
}

@Test
public void testTwo() {
// empty body
}
}

0 comments on commit 3f69fe7

Please sign in to comment.