This is an automated email from the ASF dual-hosted git repository. jsedding pushed a commit to branch SLING-9935-junit5-fallback-not-working in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git
commit f7b07efea1911cacc1157bbce2ed5f9e78ae1eb0 Author: Julian Sedding <[email protected]> AuthorDate: Thu Dec 3 11:50:40 2020 +0100 SLING-9935 - NoClassDefFoundError org/junit/platform/engine/TestEngine --- .../org/apache/sling/junit/impl/TestsManagerImpl.java | 4 ++-- .../servlet/junit5/JUnit5TestExecutionStrategy.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java b/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java index 80b8f7e..d895ce3 100644 --- a/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java +++ b/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java @@ -72,9 +72,9 @@ public class TestsManagerImpl implements TestsManager { bundleContext = ctx; testsProviderTracker = new ServiceTracker<>(bundleContext, TestsProvider.class, null); testsProviderTracker.open(); - try { + if (JUnit5TestExecutionStrategy.canLoadRequiredClasses()) { executionStrategy = new JUnit5TestExecutionStrategy(this, ctx); - } catch (NoClassDefFoundError e) { + } else { // (some) optional imports to org.junit.platform.* (JUnit5 API) are missing executionStrategy = new JUnit4TestExecutionStrategy(this); } diff --git a/src/main/java/org/apache/sling/junit/impl/servlet/junit5/JUnit5TestExecutionStrategy.java b/src/main/java/org/apache/sling/junit/impl/servlet/junit5/JUnit5TestExecutionStrategy.java index 89a6a1e..52c4c00 100644 --- a/src/main/java/org/apache/sling/junit/impl/servlet/junit5/JUnit5TestExecutionStrategy.java +++ b/src/main/java/org/apache/sling/junit/impl/servlet/junit5/JUnit5TestExecutionStrategy.java @@ -31,12 +31,30 @@ import org.junit.platform.launcher.core.LauncherFactory; import org.junit.runner.notification.RunListener; import org.osgi.framework.BundleContext; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.stream.Stream; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod; public class JUnit5TestExecutionStrategy implements TestExecutionStrategy { + // we assume that if we can load these two classes, the setup is ok for junit 5 + private static final List<String> REQUIRED_CLASSES = Collections.unmodifiableList(Arrays.asList( + "org.junit.platform.engine.TestEngine", + "org.junit.platform.launcher.Launcher" + )); + + public static boolean canLoadRequiredClasses() { + final ClassLoader classLoader = JUnit5TestExecutionStrategy.class.getClassLoader(); + return REQUIRED_CLASSES.stream() + .allMatch(name -> { + String path = name.replace('.', '/').concat(".class"); + return classLoader.getResource(path) != null; + }); + } + private final TestsManagerImpl testsManager; private final TestEngineTracker testEngineTracker;
