Skip to content

Commit 2d373c3

Browse files
authored
Added TestsShouldNotBePublic to JUnit5BestPractices, and added BeforeAll/AfterAll to TestsShouldNotBePublic scope (#286)
1 parent 64bd7e1 commit 2d373c3

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String getDisplayName() {
5858

5959
@Override
6060
public String getDescription() {
61-
return "Remove `public` and optionally `protected` modifiers from methods with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestFactory`, `@BeforeEach` or `@AfterEach`. They no longer have to be public visibility to be usable by JUnit 5.";
61+
return "Remove `public` and optionally `protected` modifiers from methods with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestFactory`, `@BeforeEach`, `@AfterEach`, `@BeforeAll`, or `@AfterAll`. They no longer have to be public visibility to be usable by JUnit 5.";
6262
}
6363

6464
@Override
@@ -154,7 +154,9 @@ private boolean hasJUnit5MethodAnnotation(MethodDeclaration method) {
154154
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest")
155155
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")
156156
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.AfterEach")
157-
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeEach")) {
157+
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeEach")
158+
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.AfterAll")
159+
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeAll")) {
158160
return true;
159161
}
160162
}

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ recipeList:
2525
- org.openrewrite.java.testing.junit5.StaticImports
2626
- org.openrewrite.java.testing.junit5.JUnit4to5Migration
2727
- org.openrewrite.java.testing.junit5.CleanupAssertions
28+
- org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic
2829
---
2930
type: specs.openrewrite.org/v1beta/recipe
3031
name: org.openrewrite.java.testing.junit5.StaticImports

src/test/java/org/openrewrite/java/testing/junit5/AssertToAssertionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void staticallyImportAssertions() {
316316
spec -> spec
317317
.cycles(3)
318318
.recipe(Environment.builder()
319-
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
319+
.scanRuntimeClasspath("org.openrewrite.java.testing")
320320
.build()
321321
.activateRecipes("org.openrewrite.java.testing.junit5.JUnit5BestPractices")),
322322
//language=java

src/test/java/org/openrewrite/java/testing/junit5/AssumeToAssumptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void defaults(RecipeSpec spec) {
3737
void assumeToAssumptions() {
3838
rewriteRun(
3939
spec -> spec.recipe(Environment.builder()
40-
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
40+
.scanRuntimeClasspath("org.openrewrite.java.testing")
4141
.build()
4242
.activateRecipes("org.openrewrite.java.testing.junit5.JUnit5BestPractices")),
4343
//language=java

src/test/java/org/openrewrite/java/testing/junit5/JUnit5BestPracticesTest.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) {
3131
spec
3232
.parser(JavaParser.fromJavaVersion().classpath("junit"))
3333
.recipe(Environment.builder()
34-
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
34+
.scanRuntimeClasspath("org.openrewrite.java.testing")
3535
.build()
3636
.activateRecipes("org.openrewrite.java.testing.junit5.JUnit5BestPractices"));
3737
}
@@ -53,9 +53,9 @@ public void initialize() {
5353
"""
5454
import org.junit.jupiter.api.BeforeEach;
5555
56-
public class Example {
56+
class Example {
5757
@BeforeEach
58-
public void initialize() {
58+
void initialize() {
5959
}
6060
}
6161
"""
@@ -80,9 +80,9 @@ public void initialize() {
8080
"""
8181
import org.junit.jupiter.api.AfterEach;
8282
83-
public class Example {
83+
class Example {
8484
@AfterEach
85-
public void initialize() {
85+
void initialize() {
8686
}
8787
}
8888
"""
@@ -107,9 +107,9 @@ public static void initialize() {
107107
"""
108108
import org.junit.jupiter.api.BeforeAll;
109109
110-
public class Example {
110+
class Example {
111111
@BeforeAll
112-
public static void initialize() {
112+
static void initialize() {
113113
}
114114
}
115115
"""
@@ -134,9 +134,9 @@ public static void initialize() {
134134
"""
135135
import org.junit.jupiter.api.AfterAll;
136136
137-
public class Example {
137+
class Example {
138138
@AfterAll
139-
public static void initialize() {
139+
static void initialize() {
140140
}
141141
}
142142
"""
@@ -152,20 +152,34 @@ void changeIgnoreToDisabled() {
152152
java(
153153
"""
154154
import org.junit.Ignore;
155+
import org.junit.Test;
155156
156157
public class Example {
157-
@Ignore @Test public void something() {}
158+
@Ignore
159+
@Test
160+
public void something() {
161+
}
158162
159-
@Ignore("not ready yet") @Test public void somethingElse() {}
163+
@Ignore("not ready yet")
164+
@Test
165+
public void somethingElse() {
166+
}
160167
}
161168
""",
162169
"""
163170
import org.junit.jupiter.api.Disabled;
171+
import org.junit.jupiter.api.Test;
164172
165-
public class Example {
166-
@Disabled @Test public void something() {}
173+
class Example {
174+
@Disabled
175+
@Test
176+
void something() {
177+
}
167178
168-
@Disabled("not ready yet") @Test public void somethingElse() {}
179+
@Disabled("not ready yet")
180+
@Test
181+
void somethingElse() {
182+
}
169183
}
170184
"""
171185
)

src/test/java/org/openrewrite/java/testing/junit5/StaticImportsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void defaults(RecipeSpec spec) {
3030
spec
3131
.parser(JavaParser.fromJavaVersion().classpath("junit-jupiter-api"))
3232
.recipe(Environment.builder()
33-
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
33+
.scanRuntimeClasspath("org.openrewrite.java.testing")
3434
.build()
3535
.activateRecipes("org.openrewrite.java.testing.junit5.JUnit5BestPractices"));
3636
}

0 commit comments

Comments
 (0)