@Test public void isolated_class_loader_cannot_load_classes_if_no_code_source_path() throws Exception { // given ClassLoader cl = isolatedClassLoader().withPrivateCopyOf(CLASS_NAME_USING_INTERFACE).build(); // when try { cl.loadClass(CLASS_NAME_USING_INTERFACE); fail(); } catch (ClassNotFoundException e) { // then assertThat(e.getMessage()).contains(CLASS_NAME_USING_INTERFACE); } }
@Test public void cannot_load_a_class_file_not_in_parent() throws Exception { // given ClassLoader cl = ClassLoaders.inMemoryClassLoader().withParent(jdkClassLoader()).build(); cl.loadClass("java.lang.String"); try { // when cl.loadClass("org.mockito.Mockito"); fail("should have not found Mockito class"); } catch (ClassNotFoundException e) { // then assertThat(e.getMessage()).contains("org.mockito.Mockito"); } }
@Test public void can_not_load_a_class_not_previously_registered_in_builder() throws Exception { // given ClassLoader cl = ClassLoaders.inMemoryClassLoader() .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude")) .build(); // when try { cl.loadClass("not.Defined"); fail(); } catch (ClassNotFoundException e) { // then assertThat(e.getMessage()).contains("not.Defined"); } }
@Test public void excluding_class_loader_cannot_load_class_when_excluded_prefix_match_class_to_load() throws Exception { // given ClassLoader cl = excludingClassLoader() .withCodeSourceUrlOf(Mockito.class) .without("org.mockito.BDDMockito") .build(); cl.loadClass("org.mockito.Mockito"); // when try { cl.loadClass("org.mockito.BDDMockito"); fail("should have raise a ClassNotFoundException"); } catch (ClassNotFoundException e) { assertThat(e.getMessage()).contains("org.mockito.BDDMockito"); } // then class successfully loaded }