### Question:
LocalClassLoader extends URLClassLoader { @Override protected Class<?> loadClass(String name, boolean initialize) throws ClassNotFoundException { try { return super.loadClass(name, initialize); } catch (ClassNotFoundException e) { return delegateLoadClassCall.loadClass(name, initialize); } } LocalClassLoader(URL[] urls, ClassLoader parent); @Override URL getResource(String s);  }### Answer:
@Test public void loadsClassFromUrlIfNotInParent() throws Exception { ClassLoader ldr = makeLocalLoaderForClass(AutonomousCodeInTestJar.class, rootClassLoader); Class<?> loaded = ldr.loadClass(AUTONOMOUS_CLASS_NAME); assertThat(loaded).isNotNull(); assertThat(loaded).isNotSameAs(AutonomousCodeInTestJar.class); }
@Test public void loadsClassFromUrlEvenIfInParent() throws Exception { ClassLoader ldr = makeLocalLoaderForClass( AutonomousCodeInTestJar.class, makeStdLoaderForClass(AutonomousCodeInTestJar.class, rootClassLoader)); Class<?> loaded = ldr.loadClass(AUTONOMOUS_CLASS_NAME); assertThat(loaded).isNotNull(); assertThat(loaded).isNotSameAs(AutonomousCodeInTestJar.class); }
@Test public void loadsClassFromParentIfNotInUrl() throws Exception { ClassLoader ldr = makeLocalLoaderForClass( ClassNoOneLoads.class, makeStdLoaderForClass(AutonomousCodeInTestJar.class, rootClassLoader)); Class<?> loaded = ldr.loadClass(AUTONOMOUS_CLASS_NAME); assertThat(loaded).isNotNull(); assertThat(loaded).isNotSameAs(AutonomousCodeInTestJar.class); }
@Test public void failsToLoadClassIfIsNeitherInParentNorInUrl() throws Exception { ClassLoader ldr = makeLocalLoaderForClass( ClassNoOneLoads.class, makeStdLoaderForClass(ClassNoOneLoads.class, rootClassLoader)); try { ldr.loadClass(AUTONOMOUS_CLASS_NAME); fail(); } catch (ClassNotFoundException e) { } }
@Test public void loadsClassWithDependencyOnParent() throws Exception { ClassLoader ldr = makeLocalLoaderForClass( ClassWithDependency.class, makeStdLoaderForClass(AutonomousCodeInTestJar.class, rootClassLoader)); Class<?> cls = ldr.loadClass(CLASS_WITH_DEPENDENCY_NAME); cls.newInstance(); }