From 0c377c2d3daacfe053f3b97701811d4b946366a1 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Fri, 11 Jun 2021 16:54:48 +0200 Subject: [PATCH 1/2] Ensure that tests are package private when using JUnit 5. --- src/test/java/edu/hm/hafner/util/ArchitectureRules.java | 6 +++--- src/test/java/edu/hm/hafner/util/ArchitectureTest.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/edu/hm/hafner/util/ArchitectureRules.java b/src/test/java/edu/hm/hafner/util/ArchitectureRules.java index 45a4494c..b33725ee 100644 --- a/src/test/java/edu/hm/hafner/util/ArchitectureRules.java +++ b/src/test/java/edu/hm/hafner/util/ArchitectureRules.java @@ -40,13 +40,13 @@ private ArchitectureRules() { .and().doNotHaveModifier(JavaModifier.ABSTRACT) .should().bePublic(); - /** Junit 5 test methods should not be public. */ - public static final ArchRule NO_PUBLIC_TEST_METHODS = + /** Junit 5 test methods should be package private. */ + public static final ArchRule ONLY_PACKAGE_PRIVATE_TEST_METHODS = methods().that().areAnnotatedWith(Test.class) .or().areAnnotatedWith(ParameterizedTest.class) .and().areDeclaredInClassesThat() .haveSimpleNameEndingWith("Test") - .should().notBePublic(); + .should().bePackagePrivate(); /** ArchUnit tests should not be public. */ public static final ArchRule NO_PUBLIC_ARCHITECTURE_TESTS = diff --git a/src/test/java/edu/hm/hafner/util/ArchitectureTest.java b/src/test/java/edu/hm/hafner/util/ArchitectureTest.java index ec37de92..c39d6611 100644 --- a/src/test/java/edu/hm/hafner/util/ArchitectureTest.java +++ b/src/test/java/edu/hm/hafner/util/ArchitectureTest.java @@ -16,7 +16,7 @@ class ArchitectureTest { static final ArchRule NO_PUBLIC_TEST_CLASSES = ArchitectureRules.NO_PUBLIC_TEST_CLASSES; @ArchTest - static final ArchRule NO_PUBLIC_TEST_METHODS = ArchitectureRules.NO_PUBLIC_TEST_METHODS; + static final ArchRule ONLY_PACKAGE_PRIVATE_TEST_METHODS = ArchitectureRules.ONLY_PACKAGE_PRIVATE_TEST_METHODS; @ArchTest static final ArchRule NO_PUBLIC_ARCHITECTURE_TESTS = ArchitectureRules.NO_PUBLIC_ARCHITECTURE_TESTS; From 2712066fbfc34d125aed27614451b7f4310755b4 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Fri, 11 Jun 2021 16:57:17 +0200 Subject: [PATCH 2/2] Exclude JUnit5TestShouldBePackagePrivate. We do have an architecture test. --- etc/pmd-configuration.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/etc/pmd-configuration.xml b/etc/pmd-configuration.xml index 25ac0df4..c8b8981a 100644 --- a/etc/pmd-configuration.xml +++ b/etc/pmd-configuration.xml @@ -13,6 +13,7 @@ +