How to use jdkClassLoader method of org.mockitoutil.ClassLoaders class

Best Mockito code snippet using org.mockitoutil.ClassLoaders.jdkClassLoader

Source:ClassLoadersTest.java Github

copy

Full Screen

...11import static org.junit.Assert.fail;12import static org.mockitoutil.ClassLoaders.currentClassLoader;13import static org.mockitoutil.ClassLoaders.excludingClassLoader;14import static org.mockitoutil.ClassLoaders.isolatedClassLoader;15import static org.mockitoutil.ClassLoaders.jdkClassLoader;16public class ClassLoadersTest {17 public static final String CLASS_NAME_DEPENDING_ON_INTERFACE = "org.mockitoutil.ClassLoadersTest$ClassUsingInterface1";18 public static final String INTERFACE_NAME = "org.mockitoutil.ClassLoadersTest$Interface1";19 @Test(expected = ClassNotFoundException.class)20 public void isolated_class_loader_cannot_load_classes_when_no_given_prefix() throws Exception {21 // given22 ClassLoader cl = isolatedClassLoader().build();23 // when24 cl.loadClass("org.mockito.Mockito");25 // then raises CNFE26 }27 @Test28 public void isolated_class_loader_cannot_load_classes_if_no_code_source_path() throws Exception {29 // given30 ClassLoader cl = isolatedClassLoader()31 .withPrivateCopyOf(CLASS_NAME_DEPENDING_ON_INTERFACE)32 .build();33 // when34 try {35 cl.loadClass(CLASS_NAME_DEPENDING_ON_INTERFACE);36 fail();37 } catch (ClassNotFoundException e) {38 // then39 assertThat(e).hasMessageContaining(CLASS_NAME_DEPENDING_ON_INTERFACE);40 }41 }42 @Test43 public void isolated_class_loader_cannot_load_classes_if_dependent_classes_do_not_match_the_prefixes() throws Exception {44 // given45 ClassLoader cl = isolatedClassLoader()46 .withCurrentCodeSourceUrls()47 .withPrivateCopyOf(CLASS_NAME_DEPENDING_ON_INTERFACE)48 .build();49 // when50 try {51 cl.loadClass(CLASS_NAME_DEPENDING_ON_INTERFACE);52 fail();53 } catch (NoClassDefFoundError e) {54 // then55 assertThat(e).hasMessageContaining("org/mockitoutil/ClassLoadersTest$Interface1");56 }57 }58 @Test59 public void isolated_class_loader_can_load_classes_when_dependent_classes_are_matching_the_prefixes() throws Exception {60 // given61 ClassLoader cl = isolatedClassLoader()62 .withCurrentCodeSourceUrls()63 .withPrivateCopyOf(CLASS_NAME_DEPENDING_ON_INTERFACE)64 .withPrivateCopyOf(INTERFACE_NAME)65 .build();66 // when67 Class<?> aClass = cl.loadClass(CLASS_NAME_DEPENDING_ON_INTERFACE);68 // then69 assertThat(aClass).isNotNull();70 assertThat(aClass.getClassLoader()).isEqualTo(cl);71 assertThat(aClass.getInterfaces()[0].getClassLoader()).isEqualTo(cl);72 }73 @Test74 public void isolated_class_loader_can_load_classes_isolated_classes_in_isolation() throws Exception {75 // given76 ClassLoader cl = isolatedClassLoader()77 .withCurrentCodeSourceUrls()78 .withPrivateCopyOf(ClassLoadersTest.class.getPackage().getName())79 .build();80 // when81 Class<?> aClass = cl.loadClass(AClass.class.getName());82 // then83 assertThat(aClass).isNotNull();84 assertThat(aClass).isNotSameAs(AClass.class);85 assertThat(aClass.getClassLoader()).isEqualTo(cl);86 }87 @Test88 public void isolated_class_loader_cannot_load_classes_if_prefix_excluded() throws Exception {89 // given90 ClassLoader cl = isolatedClassLoader()91 .withCurrentCodeSourceUrls()92 .withPrivateCopyOf(ClassLoadersTest.class.getPackage().getName())93 .without(AClass.class.getName())94 .build();95 // when96 try {97 cl.loadClass(AClass.class.getName());98 fail();99 } catch (ClassNotFoundException e) {100 // then101 assertThat(e).hasMessageContaining("org.mockitoutil")102 .hasMessageContaining(AClass.class.getName());103 }104 }105 @Test106 public void isolated_class_loader_has_no_parent() throws Exception {107 ClassLoader cl = isolatedClassLoader()108 .withCurrentCodeSourceUrls()109 .withPrivateCopyOf(CLASS_NAME_DEPENDING_ON_INTERFACE)110 .withPrivateCopyOf(INTERFACE_NAME)111 .build();112 assertThat(cl.getParent()).isNull();113 }114 @Test(expected = ClassNotFoundException.class)115 public void excluding_class_loader_cannot_load_classes_when_no_correct_source_url_set() throws Exception {116 // given117 ClassLoader cl = excludingClassLoader()118 .withCodeSourceUrlOf(this.getClass())119 .build();120 // when121 cl.loadClass("org.mockito.Mockito");122 // then class CNFE123 }124 @Test125 public void excluding_class_loader_can_load_classes_when_correct_source_url_set() throws Exception {126 // given127 ClassLoader cl = excludingClassLoader()128 .withCodeSourceUrlOf(Mockito.class)129 .build();130 // when131 cl.loadClass("org.mockito.Mockito");132 // then class successfully loaded133 }134 @Test135 public void excluding_class_loader_cannot_load_class_when_excluded_prefix_match_class_to_load() throws Exception {136 // given137 ClassLoader cl = excludingClassLoader()138 .withCodeSourceUrlOf(Mockito.class)139 .without("org.mockito.BDDMockito")140 .build();141 cl.loadClass("org.mockito.Mockito");142 // when143 try {144 cl.loadClass("org.mockito.BDDMockito");145 fail("should have raise a ClassNotFoundException");146 } catch (ClassNotFoundException e) {147 assertThat(e.getMessage()).contains("org.mockito.BDDMockito");148 }149 // then class successfully loaded150 }151 @Test152 public void can_not_load_a_class_not_previously_registered_in_builder() throws Exception {153 // given154 ClassLoader cl = ClassLoaders155 .inMemoryClassLoader()156 .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))157 .build();158 // when159 try {160 cl.loadClass("not.Defined");161 fail();162 } catch (ClassNotFoundException e) {163 // then164 assertThat(e.getMessage()).contains("not.Defined");165 }166 }167 @Test168 public void can_load_a_class_in_memory_from_bytes() throws Exception {169 // given170 ClassLoader cl = ClassLoaders171 .inMemoryClassLoader()172 .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))173 .build();174 // when175 Class<?> aClass = cl.loadClass("yop.Dude");176 // then177 assertThat(aClass).isNotNull();178 assertThat(aClass.getClassLoader()).isEqualTo(cl);179 assertThat(aClass.getName()).isEqualTo("yop.Dude");180 }181 @Test182 public void cannot_load_a_class_file_not_in_parent() throws Exception {183 // given184 ClassLoader cl = ClassLoaders185 .inMemoryClassLoader()186 .withParent(jdkClassLoader())187 .build();188 cl.loadClass("java.lang.String");189 try {190 // when191 cl.loadClass("org.mockito.Mockito");192 fail("should have not found Mockito class");193 } catch (ClassNotFoundException e) {194 // then195 assertThat(e.getMessage()).contains("org.mockito.Mockito");196 }197 }198 @Test199 public void can_list_all_classes_reachable_in_a_classloader() throws Exception {200 ClassLoader classLoader = ClassLoaders.inMemoryClassLoader()201 .withParent(jdkClassLoader())202 .withClassDefinition("a.A", SimpleClassGenerator.makeMarkerInterface("a.A"))203 .withClassDefinition("a.b.B", SimpleClassGenerator.makeMarkerInterface("a.b.B"))204 .withClassDefinition("c.C", SimpleClassGenerator.makeMarkerInterface("c.C"))205// .withCodeSourceUrlOf(ClassLoaders.class)206 .build();207 assertThat(ClassLoaders.in(classLoader).listOwnedClasses()).containsOnly("a.A", "a.b.B", "c.C");208 assertThat(ClassLoaders.in(classLoader).omit("b", "c").listOwnedClasses()).containsOnly("a.A");209 }210 @Test211 public void return_bootstrap_classloader() throws Exception {212 assertThat(jdkClassLoader()).isNotEqualTo(Mockito.class.getClassLoader());213 assertThat(jdkClassLoader()).isNotEqualTo(ClassLoaders.class.getClassLoader());214 assertThat(jdkClassLoader()).isEqualTo(Number.class.getClassLoader());215 assertThat(jdkClassLoader()).isEqualTo(null);216 }217 @Test218 public void return_current_classloader() throws Exception {219 assertThat(currentClassLoader()).isEqualTo(this.getClass().getClassLoader());220 }221 @Test222 public void can_run_in_given_classloader() throws Exception {223 // given224 final ClassLoader cl = isolatedClassLoader()225 .withCurrentCodeSourceUrls()226 .withCodeSourceUrlOf(Assertions.class)227 .withPrivateCopyOf("org.assertj.core")228 .withPrivateCopyOf(ClassLoadersTest.class.getPackage().getName())229 .without(AClass.class.getName())...

Full Screen

Full Screen

jdkClassLoader

Using AI Code Generation

copy

Full Screen

1public static ClassLoader jdkClassLoader(ClassLoader classLoader)2public static ClassLoader inMemoryClassLoader(Class<?>... classes)3public static ClassLoader inMemoryClassLoader(Class<?>... classes)4public static ClassLoader inMemoryClassLoader(Class<?>[] classes, Class<?>[] interfaces)5public static ClassLoader inMemoryClassLoader(Class<?>[] classes, Class<?>[] interfaces, Class<?>[] enums)6public static ClassLoader inMemoryClassLoader(Class<?>[] classes, Class<?>[] interfaces, Class<?>[] enums, Class<?>[] annotations)

Full Screen

Full Screen

jdkClassLoader

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.junit.runner.RunWith;3import org.mockito.runners.MockitoJUnitRunner;4import org.mockitoutil.ClassLoaders;5import java.io.File;6import static org.junit.Assert.assertEquals;7@RunWith(MockitoJUnitRunner.class)8public class ClassLoadersTest {9 public void testClassLoaders() throws Exception {10 File file = new File("target/test-classes/jarTest.jar");11 ClassLoader classLoader = ClassLoaders.jdkClassLoader(file);12 Class<?> clazz = classLoader.loadClass("com.example.ClassToTest");13 Object instance = clazz.newInstance();14 assertEquals("com.example.ClassToTest", instance.toString());15 }16}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful