Ensure CompileWithTargetClassAccessExtension only uses Jupiter TestEngine

Commit 9dd7f5412a (which has now been
reverted) addressed the issue of having the TestNG TestEngine for the
JUnit Platform on the test runtime classpath by allowing `org.testng`
types to pass through to the original ClassLoader; however, that fix
merely obfuscated the underlying issue.

The underlying issue is that the CompileWithTargetClassAccessExtension
is only applicable to JUnit Jupiter tests and therefore should launch
the JUnit Platform with only the JUnit Jupiter TestEngine active.

This commit addresses this issue by applying an EngineFilter to include
only the "junit-jupiter" test engine.

Closes gh-28900
This commit is contained in:
Sam Brannen 2022-08-01 12:48:36 +03:00
parent b89b6bd7a8
commit 4896b115d2
1 changed files with 3 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.EngineFilter;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestPlan;
@ -40,6 +41,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Christoph Dreis
* @author Phillip Webb
* @author Sam Brannen
* @since 6.0
*/
class CompileWithTargetClassAccessExtension implements InvocationInterceptor {
@ -134,6 +136,7 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor {
Method testMethod = findMethod(testClass, testMethodName);
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod))
.filters(EngineFilter.includeEngines("junit-jupiter"))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);