-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid JupiterTestEngine crash if Hamcrest is not on the classpath
Prior to this commit, the JupiterTestEngine would crash without executing any tests if JUnit 4 was on the classpath but Hamcrest was not. This commit fixes this bug by ensuring that initialization of the OpenTest4JAndJUnit4AwareThrowableCollector class no longer fails if the org.junit.internal.AssumptionViolatedException class cannot be loaded from the classpath -- for example, due to a missing Hamcrest dependency. Fixes #2004
- Loading branch information
Showing
3 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ava/org/junit/jupiter/engine/support/OpenTest4JAndJUnit4AwareThrowableCollectorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2015-2019 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.support; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
import org.junit.internal.AssumptionViolatedException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
|
||
/** | ||
* Unit tests for {@link OpenTest4JAndJUnit4AwareThrowableCollector}. | ||
* | ||
* @since 5.6 | ||
*/ | ||
class OpenTest4JAndJUnit4AwareThrowableCollectorTests { | ||
|
||
@Test | ||
void simulateHamcrestNotInTheClasspath() throws Exception { | ||
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); | ||
try { | ||
HamcrestHidingClassLoader classLoader = new HamcrestHidingClassLoader(); | ||
|
||
// We have to set our custom ClassLoader as the TCCL so that | ||
// ReflectionUtils uses it (indirectly via ClassLoaderUtils). | ||
Thread.currentThread().setContextClassLoader(classLoader); | ||
|
||
// Ensure that our custom ClassLoader actually throws a NoClassDefFoundError | ||
// when attempting to load the AssumptionViolatedException class. | ||
assertThrows(NoClassDefFoundError.class, | ||
() -> ReflectionUtils.tryToLoadClass(AssumptionViolatedException.class.getName())); | ||
|
||
Class<?> clazz = classLoader.loadClass(OpenTest4JAndJUnit4AwareThrowableCollector.class.getName()); | ||
assertNotNull(ReflectionUtils.newInstance(clazz)); | ||
} | ||
finally { | ||
Thread.currentThread().setContextClassLoader(originalClassLoader); | ||
} | ||
} | ||
|
||
private static class HamcrestHidingClassLoader extends URLClassLoader { | ||
|
||
HamcrestHidingClassLoader() { | ||
super(new URL[] { | ||
OpenTest4JAndJUnit4AwareThrowableCollector.class.getProtectionDomain().getCodeSource().getLocation() }, | ||
getSystemClassLoader()); | ||
} | ||
|
||
@Override | ||
public Class<?> loadClass(String name) throws ClassNotFoundException { | ||
|
||
// Load a new instance of the OpenTest4JAndJUnit4AwareThrowableCollector class | ||
if (name.equals(OpenTest4JAndJUnit4AwareThrowableCollector.class.getName())) { | ||
return findClass(name); | ||
} | ||
|
||
// Simulate that Hamcrest is not in the classpath when loading AssumptionViolatedException | ||
if (name.equals(AssumptionViolatedException.class.getName())) { | ||
throw new NoClassDefFoundError("org/hamcrest/SelfDescribing"); | ||
} | ||
|
||
// Else | ||
return super.loadClass(name); | ||
} | ||
|
||
} | ||
|
||
} |