Skip to content

Commit ab2ecf7

Browse files
committed
Polish "Check that JUnit 5 test classes are package-private"
- Clear the new fields each time a new tree is begun. This prevents state from one tree affecting the checking of another. - Allow abstract test classes to be public Closes gh-281
1 parent 09679cc commit ab2ecf7

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ public int[] getAcceptableTokens() {
9797

9898
@Override
9999
public void beginTree(DetailAST rootAST) {
100+
this.testClass = null;
100101
this.imports.clear();
101102
this.testMethods.clear();
102103
this.lifecycleMethods.clear();
104+
this.nestedTestClasses.clear();
103105
}
104106

105107
@Override
@@ -181,7 +183,7 @@ private boolean shouldCheck() {
181183
}
182184

183185
private void check() {
184-
if (this.testClass != null) {
186+
if (this.testClass != null && !isAbstract(this.testClass)) {
185187
checkVisibility(Arrays.asList(this.testClass), "junit5.publicClass", null);
186188
}
187189
checkVisibility(this.nestedTestClasses, "junit5.publicNestedClass", "junit5.privateNestedClass");
@@ -200,6 +202,11 @@ private void check() {
200202
checkVisibility(this.lifecycleMethods, "junit5.lifecyclePublicMethod", "junit5.lifecyclePrivateMethod");
201203
}
202204

205+
private boolean isAbstract(DetailAST ast) {
206+
DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
207+
return modifiers.findFirstToken(TokenTypes.ABSTRACT) != null;
208+
}
209+
203210
private void checkVisibility(List<DetailAST> asts, String publicMessageKey, String privateMessageKey) {
204211
for (DetailAST ast : asts) {
205212
DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2017-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
/**
20+
* This is a valid example. We allow abstract test classes to be
21+
* public so that classes in other packages can extend them.
22+
*
23+
* @author Andy Wilkinson
24+
*/
25+
public abstract class JUnit5PublicAbstractIsValid {
26+
27+
@Test
28+
void doSomethingWorks() {
29+
// test here
30+
}
31+
32+
}

0 commit comments

Comments
 (0)