Skip to content

Commit 72e4a5f

Browse files
committed
Fix precondition check in TestClassScanner and improve Javadoc
- remove not-empty precondition check for packageNames so that the core scan() method can actually be used. - document constructor - document use case for packageNames - add test that scan's all test classes in the spring-test project - reduce logging due to the previous action item See spring-projectsgh-28824
1 parent 3fd7265 commit 72e4a5f

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/aot/TestClassScanner.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ class TestClassScanner {
9494
private final Set<Path> classpathRoots;
9595

9696

97+
/**
98+
* Create a {@code TestClassScanner} for the given classpath roots.
99+
* <p>For example, in a Gradle project that only supports Java-based tests,
100+
* the supplied set would contain a single {@link Path} representing the
101+
* absolute path to the project's {@code build/classes/java/test} folder.
102+
* @param classpathRoots the classpath roots to scan
103+
*/
97104
TestClassScanner(Set<Path> classpathRoots) {
98105
Assert.notEmpty(classpathRoots, "'classpathRoots' must not be null or empty");
99106
Assert.noNullElements(classpathRoots, "'classpathRoots' must not contain null elements");
@@ -111,9 +118,12 @@ Stream<Class<?>> scan() {
111118
/**
112119
* Scan the configured classpath roots for Spring integration test classes
113120
* in the given packages.
121+
* <p>This method is currently only intended to be used within our own test
122+
* suite to validate the behavior of this scanner with a limited scope. In
123+
* production scenarios one should invoke {@link #scan()} to scan all packages
124+
* in the configured classpath roots.
114125
*/
115126
Stream<Class<?>> scan(String... packageNames) {
116-
Assert.notEmpty(packageNames, "'packageNames' must not be null or empty");
117127
Assert.noNullElements(packageNames, "'packageNames' must not contain null elements");
118128

119129
if (logger.isInfoEnabled()) {

spring-test/src/test/java/org/springframework/test/context/aot/TestClassScannerTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,22 @@ void scanTestSuitesWithNestedSuites() {
8787
);
8888
}
8989

90+
@Test
91+
void scanEntireSpringTestModule() {
92+
assertThat(scan()).hasSizeGreaterThan(400);
93+
}
94+
95+
private Stream<Class<?>> scan() {
96+
return new TestClassScanner(classpathRoots()).scan();
97+
}
98+
9099
private Stream<Class<?>> scan(String... packageNames) {
100+
return new TestClassScanner(classpathRoots()).scan(packageNames);
101+
}
102+
103+
private Set<Path> classpathRoots() {
91104
try {
92-
Set<Path> classpathRoots = Set.of(
93-
Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()));
94-
return new TestClassScanner(classpathRoots).scan(packageNames);
105+
return Set.of(Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()));
95106
}
96107
catch (Exception ex) {
97108
throw new RuntimeException(ex);

spring-test/src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Logger name="org.springframework.test.context.TestContext" level="warn" />
1515
<Logger name="org.springframework.test.context.TestContextManager" level="warn" />
1616
<Logger name="org.springframework.test.context.ContextLoaderUtils" level="warn" />
17-
<Logger name="org.springframework.test.context.aot" level="trace" />
17+
<Logger name="org.springframework.test.context.aot" level="debug" />
1818
<Logger name="org.springframework.test.context.cache" level="warn" />
1919
<Logger name="org.springframework.test.context.junit4.rules" level="warn" />
2020
<Logger name="org.springframework.test.context.transaction.TransactionalTestExecutionListener" level="warn" />

0 commit comments

Comments
 (0)