Skip to content

Commit

Permalink
Polish CompileWithTargetClassAccessExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 1, 2022
1 parent 4896b11 commit 8fb27c3
Showing 1 changed file with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@
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;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.launcher.EngineFilter.includeEngines;

/**
* JUnit {@link InvocationInterceptor} to support
* JUnit Jupiter {@link InvocationInterceptor} to support
* {@link CompileWithTargetClassAccess @CompileWithTargetClassAccess}.
*
* @author Christoph Dreis
Expand Down Expand Up @@ -129,22 +128,20 @@ private void runTestWithModifiedClassPath(
}
}

private void runTest(ClassLoader classLoader, String testClassName,
String testMethodName) throws Throwable {
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName)
throws Throwable {

Class<?> testClass = classLoader.loadClass(testClassName);
Method testMethod = findMethod(testClass, testMethodName);
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod))
.filters(EngineFilter.includeEngines("junit-jupiter"))
.selectors(selectMethod(testClass, testMethod))
.filters(includeEngines("junit-jupiter"))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(testPlan);
Launcher launcher = LauncherFactory.create();
launcher.execute(request, listener);
TestExecutionSummary summary = listener.getSummary();
if (!CollectionUtils.isEmpty(summary.getFailures())) {
if (summary.getTotalFailureCount() > 0) {
throw summary.getFailures().get(0).getException();
}
}
Expand All @@ -167,8 +164,7 @@ private Method findMethod(Class<?> testClass, String testMethodName) {
@FunctionalInterface
interface Action {

static Action NONE = () -> {
};
Action NONE = () -> {};


void run() throws Throwable;
Expand Down

0 comments on commit 8fb27c3

Please sign in to comment.