IsolatedClassloader extends URLClassLoader { void addFiles(List<File> files) { try { for (File file : files) { addURL(file.toURI().toURL()); } } catch (MalformedURLException e) { throw new IllegalStateException("Fail to create classloader", e); } } IsolatedClassloader(ClassLoader parent, ClassloadRules rules); @Override URL getResource(String name); @Override Enumeration<URL> getResources(String name);  }
@Test public void add_jars() throws MalformedURLException { File f = new File("dummy"); File[] files = {f}; classLoader.addFiles(Arrays.asList(files)); assertThat(classLoader.getURLs()).contains(f.toURI().toURL()); }
@Test public void error_add_jars() throws MalformedURLException { File f = mock(File.class); URI uri = mock(URI.class); when(f.toURI()).thenReturn(uri); when(uri.toURL()).thenThrow(MalformedURLException.class); File[] files = {f}; thrown.expect(IllegalStateException.class); thrown.expectMessage("Fail to create classloader"); classLoader.addFiles(Arrays.asList(files)); }