I am trying to discover JUnit 5 tests with the help of LauncherDiscoveryRequest
as described in the user guide. The code I am using looks as follows:
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.filters(includeClassNamePatterns(".*"))
.build();
TestPlan plan = LauncherFactory.create().discover(request);
for (TestIdentifier root : plan.getRoots()) {
System.out.println("Root: " + root.toString());
for (TestIdentifier test : plan.getChildren(root)) {
System.out.println("Found test: " + test.toString());
}
}
Does LauncherDiscoveryRequestBuilder
only auto-discover tests on the classpath of the JVM running this code? I tried using the methods DiscoverySelectors.selectClasspathRoots
and DiscoverySelectors.selectClasspathResource
as selectors to provide root directories to be searched for test classes. However, I wasn't able to discover any tests. Do the parameters for selectClasspathRoots
and selectClasspathResource
have to point to the root directory containing the class files organized by package or do you provide the full path to each test class file?
I tried the following where /some/dir
represents the root directory containing test class files:
File classesDir = new File("/some/dir");
LauncherDiscoveryRequestBuilder.request()
.selectors(selectClasspathRoots(Collections.singleton(Paths.get(classesDir.toURI()))))
.build();
I had a look at LauncherDiscoveryRequestBuilderTests.java
but it wasn't very useful in figuring out why my example code doesn't work. How do I best diagnose the issue?
I am using the following dependencies:
org.junit.platform:junit-platform-engine:1.0.0-M3
org.junit.platform:junit-platform-launcher:1.0.0-M3
Short answer: Yes, the
Launcher
will only auto-discover tests on the classpath of the JVM running the code.In general,
TestEngine
implementations are responsible for resolving selectors such as theClasspathRootSelector
you create viaselectClasspathRoots()
. The Jupiter and Vintage engines will scan the supplied directories for.class
files and then try to load them using the current thread's contextClassLoader
. If the latter step fails, the class will be silently ignored.If you want to discover tests not yet on the classpath, you need to create a new
ClassLoader
and make it the current thread's contextClassLoader
. The JUnit PlatformConsoleLauncher
does this, too:ClassLoader
Launcher
with replaced contextClassLoader