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 gh-28824
This commit is contained in:
@@ -94,6 +94,13 @@ class TestClassScanner {
|
|||||||
private final Set<Path> classpathRoots;
|
private final Set<Path> classpathRoots;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@code TestClassScanner} for the given classpath roots.
|
||||||
|
* <p>For example, in a Gradle project that only supports Java-based tests,
|
||||||
|
* the supplied set would contain a single {@link Path} representing the
|
||||||
|
* absolute path to the project's {@code build/classes/java/test} folder.
|
||||||
|
* @param classpathRoots the classpath roots to scan
|
||||||
|
*/
|
||||||
TestClassScanner(Set<Path> classpathRoots) {
|
TestClassScanner(Set<Path> classpathRoots) {
|
||||||
Assert.notEmpty(classpathRoots, "'classpathRoots' must not be null or empty");
|
Assert.notEmpty(classpathRoots, "'classpathRoots' must not be null or empty");
|
||||||
Assert.noNullElements(classpathRoots, "'classpathRoots' must not contain null elements");
|
Assert.noNullElements(classpathRoots, "'classpathRoots' must not contain null elements");
|
||||||
@@ -111,9 +118,12 @@ class TestClassScanner {
|
|||||||
/**
|
/**
|
||||||
* Scan the configured classpath roots for Spring integration test classes
|
* Scan the configured classpath roots for Spring integration test classes
|
||||||
* in the given packages.
|
* in the given packages.
|
||||||
|
* <p>This method is currently only intended to be used within our own test
|
||||||
|
* suite to validate the behavior of this scanner with a limited scope. In
|
||||||
|
* production scenarios one should invoke {@link #scan()} to scan all packages
|
||||||
|
* in the configured classpath roots.
|
||||||
*/
|
*/
|
||||||
Stream<Class<?>> scan(String... packageNames) {
|
Stream<Class<?>> scan(String... packageNames) {
|
||||||
Assert.notEmpty(packageNames, "'packageNames' must not be null or empty");
|
|
||||||
Assert.noNullElements(packageNames, "'packageNames' must not contain null elements");
|
Assert.noNullElements(packageNames, "'packageNames' must not contain null elements");
|
||||||
|
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
|
|||||||
@@ -87,11 +87,22 @@ class TestClassScannerTests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void scanEntireSpringTestModule() {
|
||||||
|
assertThat(scan()).hasSizeGreaterThan(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<Class<?>> scan() {
|
||||||
|
return new TestClassScanner(classpathRoots()).scan();
|
||||||
|
}
|
||||||
|
|
||||||
private Stream<Class<?>> scan(String... packageNames) {
|
private Stream<Class<?>> scan(String... packageNames) {
|
||||||
|
return new TestClassScanner(classpathRoots()).scan(packageNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Path> classpathRoots() {
|
||||||
try {
|
try {
|
||||||
Set<Path> classpathRoots = Set.of(
|
return Set.of(Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()));
|
||||||
Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()));
|
|
||||||
return new TestClassScanner(classpathRoots).scan(packageNames);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<Logger name="org.springframework.test.context.TestContext" level="warn" />
|
<Logger name="org.springframework.test.context.TestContext" level="warn" />
|
||||||
<Logger name="org.springframework.test.context.TestContextManager" level="warn" />
|
<Logger name="org.springframework.test.context.TestContextManager" level="warn" />
|
||||||
<Logger name="org.springframework.test.context.ContextLoaderUtils" level="warn" />
|
<Logger name="org.springframework.test.context.ContextLoaderUtils" level="warn" />
|
||||||
<Logger name="org.springframework.test.context.aot" level="trace" />
|
<Logger name="org.springframework.test.context.aot" level="debug" />
|
||||||
<Logger name="org.springframework.test.context.cache" level="warn" />
|
<Logger name="org.springframework.test.context.cache" level="warn" />
|
||||||
<Logger name="org.springframework.test.context.junit4.rules" level="warn" />
|
<Logger name="org.springframework.test.context.junit4.rules" level="warn" />
|
||||||
<Logger name="org.springframework.test.context.transaction.TransactionalTestExecutionListener" level="warn" />
|
<Logger name="org.springframework.test.context.transaction.TransactionalTestExecutionListener" level="warn" />
|
||||||
|
|||||||
Reference in New Issue
Block a user