Skip to content

Commit e3d6b46

Browse files
committed
Check that JUnit 5 lifecycle methods are not public or private
Closes gh-115
1 parent 99cf509 commit e3d6b46

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringJUnit5Check.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class SpringJUnit5Check extends AbstractSpringCheck {
4343
private static final List<String> TEST_ANNOTATIONS = Collections
4444
.unmodifiableList(Arrays.asList("Test", JUNIT4_TEST_ANNOTATION, JUNIT5_TEST_ANNOTATION));
4545

46+
private static final List<String> LIFECYCLE_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList("BeforeAll",
47+
"org.junit.jupiter.api.BeforeAll", "BeforeEach", "org.junit.jupiter.api.BeforeEach", "AfterAll",
48+
"org.junit.jupiter.api.AfterAll", "AfterEach", "org.junit.jupiter.api.AfterEach"));
49+
4650
private static final List<String> BANNED_IMPORTS;
4751
static {
4852
List<String> bannedImports = new ArrayList<>();
@@ -62,6 +66,8 @@ public class SpringJUnit5Check extends AbstractSpringCheck {
6266

6367
private final Map<String, FullIdent> imports = new LinkedHashMap<>();
6468

69+
private final List<DetailAST> lifecycleMethods = new ArrayList<>();
70+
6571
@Override
6672
public int[] getAcceptableTokens() {
6773
return new int[] { TokenTypes.METHOD_DEF, TokenTypes.IMPORT };
@@ -71,6 +77,7 @@ public int[] getAcceptableTokens() {
7177
public void beginTree(DetailAST rootAST) {
7278
this.imports.clear();
7379
this.testMethods.clear();
80+
this.lifecycleMethods.clear();
7481
}
7582

7683
@Override
@@ -88,6 +95,9 @@ private void visitMethodDef(DetailAST ast) {
8895
if (AnnotationUtil.containsAnnotation(ast, TEST_ANNOTATIONS)) {
8996
this.testMethods.add(ast);
9097
}
98+
if (AnnotationUtil.containsAnnotation(ast, LIFECYCLE_ANNOTATIONS)) {
99+
this.lifecycleMethods.add(ast);
100+
}
91101
}
92102

93103
private void visitImport(DetailAST ast) {
@@ -103,7 +113,7 @@ public void finishTree(DetailAST rootAST) {
103113
}
104114

105115
private boolean shouldCheck() {
106-
if (this.testMethods.isEmpty()) {
116+
if (this.testMethods.isEmpty() && this.lifecycleMethods.isEmpty()) {
107117
return false;
108118
}
109119
for (String unlessImport : this.unlessImports) {
@@ -126,13 +136,18 @@ private void check() {
126136
log(testMethod, "junit5.bannedTestAnnotation");
127137
}
128138
}
129-
for (DetailAST testMethod : this.testMethods) {
130-
DetailAST modifiers = testMethod.findFirstToken(TokenTypes.MODIFIERS);
139+
checkMethodVisibility(this.testMethods, "junit5.testPublicMethod", "junit5.testPrivateMethod");
140+
checkMethodVisibility(this.lifecycleMethods, "junit5.lifecyclePublicMethod", "junit5.lifecyclePrivateMethod");
141+
}
142+
143+
private void checkMethodVisibility(List<DetailAST> methods, String publicMessageKey, String privateMessageKey) {
144+
for (DetailAST method : methods) {
145+
DetailAST modifiers = method.findFirstToken(TokenTypes.MODIFIERS);
131146
if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null) {
132-
log(testMethod, "junit5.publicMethod");
147+
log(method, publicMessageKey);
133148
}
134149
if (modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null) {
135-
log(testMethod, "junit5.privateMethod");
150+
log(method, privateMessageKey);
136151
}
137152
}
138153
}

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ javadoc.badCase=Javadoc element descriptions should not start with an uppercase
88
header.unexpected=Unexpected header.
99
nothis.unexpected=Reference to instance variable ''{0}'' should not use \"this.\".
1010
methodorder.outOfOrder=Method ''{0}'' is out of order, expected {1}.
11-
junit5.publicMethod="Test method ''{0}'' should not be public."
12-
junit5.privateMethod="Test method ''{0}'' should not be private."
1311
junit5.bannedImport="Import ''{0}'' should not be used in a JUnit 5 test."
1412
junit5.bannedTestAnnotation="JUnit 4 @Test annotation should not be used in a JUnit 5 test."
13+
junit5.lifecyclePublicMethod="Lifecycle method ''{0}'' should not be public."
14+
junit5.lifecyclePrivateMethod="Lifecycle method ''{0}'' should not be private."
15+
junit5.testPublicMethod="Test method ''{0}'' should not be public."
16+
junit5.testPrivateMethod="Test method ''{0}'' should not be private."
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
+Test method 'doSomethingWorks' should not be public
22
+Test method 'doSomethingElseWorks' should not be private
3+
+Lifecycle method 'publicBeforeAll' should not be public
4+
+Lifecycle method 'publicBeforeEach' should not be public
5+
+Lifecycle method 'publicAfterAll' should not be public
6+
+Lifecycle method 'publicAfterEach' should not be public
7+
+Lifecycle method 'privateBeforeAll' should not be private
8+
+Lifecycle method 'privateBeforeEach' should not be private
9+
+Lifecycle method 'privateAfterAll' should not be private
10+
+Lifecycle method 'privateAfterEach' should not be private

spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/JUnit5BadModifier.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,46 @@
2323
*/
2424
public class JUnit5BadModifier {
2525

26+
@BeforeAll
27+
public static void publicBeforeAll() {
28+
29+
}
30+
31+
@BeforeEach
32+
public void publicBeforeEach() {
33+
34+
}
35+
36+
@AfterAll
37+
public static void publicAfterAll() {
38+
39+
}
40+
41+
@BeforeEach
42+
public void publicAfterEach() {
43+
44+
}
45+
46+
@BeforeAll
47+
private static void privateBeforeAll() {
48+
49+
}
50+
51+
@BeforeEach
52+
private void privateBeforeEach() {
53+
54+
}
55+
56+
@AfterAll
57+
private static void privateAfterAll() {
58+
59+
}
60+
61+
@BeforeEach
62+
private void privateAfterEach() {
63+
64+
}
65+
2666
@Test
2767
public void doSomethingWorks() {
2868
// test here

0 commit comments

Comments
 (0)