Best Mockito code snippet using org.mockitoutil.ClassLoaders.InMemoryClassLoader
Source:ClassLoadersTest.java
1package org.mockitoutil;2import org.junit.Test;3import org.mockito.Mockito;4import static org.fest.assertions.Assertions.assertThat;5import static org.junit.Assert.fail;6import static org.mockitoutil.ClassLoaders.currentClassLoader;7import static org.mockitoutil.ClassLoaders.excludingClassLoader;8import static org.mockitoutil.ClassLoaders.isolatedClassLoader;9import static org.mockitoutil.ClassLoaders.jdkClassLoader;10public class ClassLoadersTest {11 public static final String CLASS_NAME_USING_INTERFACE = "org.mockitoutil.ClassLoadersTest$ClassUsingInterface1";12 public static final String INTERFACE_NAME = "org.mockitoutil.ClassLoadersTest$Interface1";13 @Test(expected = ClassNotFoundException.class)14 public void isolated_class_loader_cannot_load_classes_when_no_given_prefix() throws Exception {15 // given16 ClassLoader cl = isolatedClassLoader().build();17 // when18 cl.loadClass("org.mockito.Mockito");19 // then raises CNFE20 }21 @Test22 public void isolated_class_loader_cannot_load_classes_if_no_code_source_path() throws Exception {23 // given24 ClassLoader cl = isolatedClassLoader()25 .withPrivateCopyOf(CLASS_NAME_USING_INTERFACE)26 .build();27 // when28 try {29 cl.loadClass(CLASS_NAME_USING_INTERFACE);30 fail();31 } catch (ClassNotFoundException e) {32 // then33 assertThat(e.getMessage()).contains(CLASS_NAME_USING_INTERFACE);34 }35 }36 @Test37 public void isolated_class_loader_cannot_load_classes_not_matching_the_prefix() throws Exception {38 // given39 ClassLoader cl = isolatedClassLoader()40 .withCurrentCodeSourceUrls()41 .withPrivateCopyOf(CLASS_NAME_USING_INTERFACE)42 .build();43 // when44 try {45 cl.loadClass(CLASS_NAME_USING_INTERFACE);46 fail();47 } catch (NoClassDefFoundError e) {48 // then49 assertThat(e.getMessage()).contains("org/mockitoutil/ClassLoadersTest$Interface1");50 }51 }52 @Test53 public void isolated_class_loader_can_load_all_classes_unless_all_classes_mathch_the_prefixes() throws Exception {54 // given55 ClassLoader cl = isolatedClassLoader()56 .withCurrentCodeSourceUrls()57 .withPrivateCopyOf(CLASS_NAME_USING_INTERFACE)58 .withPrivateCopyOf(INTERFACE_NAME)59 .build();60 // when61 Class<?> aClass = cl.loadClass(CLASS_NAME_USING_INTERFACE);62 // then63 assertThat(aClass).isNotNull();64 assertThat(aClass.getClassLoader()).isEqualTo(cl);65 assertThat(aClass.getInterfaces()[0].getClassLoader()).isEqualTo(cl);66 }67 @Test68 public void isolated_class_loader_has_no_parent() throws Exception {69 ClassLoader cl = isolatedClassLoader()70 .withCurrentCodeSourceUrls()71 .withPrivateCopyOf(CLASS_NAME_USING_INTERFACE)72 .withPrivateCopyOf(INTERFACE_NAME)73 .build();74 assertThat(cl.getParent()).isNull();75 }76 @Test(expected = ClassNotFoundException.class)77 public void excluding_class_loader_cannot_load_classes_when_no_correct_source_url_set() throws Exception {78 // given79 ClassLoader cl = excludingClassLoader()80 .withCodeSourceUrlOf(this.getClass())81 .build();82 // when83 cl.loadClass("org.mockito.Mockito");84 // then class CNFE85 }86 @Test87 public void excluding_class_loader_can_load_classes_when_correct_source_url_set() throws Exception {88 // given89 ClassLoader cl = excludingClassLoader()90 .withCodeSourceUrlOf(Mockito.class)91 .build();92 // when93 cl.loadClass("org.mockito.Mockito");94 // then class successfully loaded95 }96 @Test97 public void excluding_class_loader_cannot_load_class_when_excluded_prefix_match_class_to_load() throws Exception {98 // given99 ClassLoader cl = excludingClassLoader()100 .withCodeSourceUrlOf(Mockito.class)101 .without("org.mockito.BDDMockito")102 .build();103 cl.loadClass("org.mockito.Mockito");104 // when105 try {106 cl.loadClass("org.mockito.BDDMockito");107 fail("should have raise a ClassNotFoundException");108 } catch (ClassNotFoundException e) {109 assertThat(e.getMessage()).contains("org.mockito.BDDMockito");110 }111 // then class successfully loaded112 }113 @Test114 public void can_not_load_a_class_not_previously_registered_in_builder() throws Exception {115 // given116 ClassLoader cl = ClassLoaders117 .inMemoryClassLoader()118 .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))119 .build();120 // when121 try {122 cl.loadClass("not.Defined");123 fail();124 } catch (ClassNotFoundException e) {125 // then126 assertThat(e.getMessage()).contains("not.Defined");127 }128 }129 @Test130 public void can_load_a_class_in_memory_from_bytes() throws Exception {131 // given132 ClassLoader cl = ClassLoaders133 .inMemoryClassLoader()134 .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))135 .build();136 // when137 Class<?> aClass = cl.loadClass("yop.Dude");138 // then139 assertThat(aClass).isNotNull();140 assertThat(aClass.getClassLoader()).isEqualTo(cl);141 assertThat(aClass.getName()).isEqualTo("yop.Dude");142 }143 @Test144 public void cannot_load_a_class_file_not_in_parent() throws Exception {145 // given146 ClassLoader cl = ClassLoaders147 .inMemoryClassLoader()148 .withParent(jdkClassLoader())149 .build();150 cl.loadClass("java.lang.String");151 try {152 // when153 cl.loadClass("org.mockito.Mockito");154 fail("should have not found Mockito class");155 } catch (ClassNotFoundException e) {156 // then157 assertThat(e.getMessage()).contains("org.mockito.Mockito");158 }159 }160 @Test161 public void can_list_all_classes_reachable_in_a_classloader() throws Exception {162 ClassLoader classLoader = ClassLoaders.inMemoryClassLoader()163 .withParent(jdkClassLoader())164 .withClassDefinition("a.A", SimpleClassGenerator.makeMarkerInterface("a.A"))165 .withClassDefinition("a.b.B", SimpleClassGenerator.makeMarkerInterface("a.b.B"))166 .withClassDefinition("c.C", SimpleClassGenerator.makeMarkerInterface("c.C"))167// .withCodeSourceUrlOf(ClassLoaders.class)168 .build();169 assertThat(ClassLoaders.in(classLoader).listOwnedClasses()).containsOnly("a.A", "a.b.B", "c.C");170 assertThat(ClassLoaders.in(classLoader).omit("b", "c").listOwnedClasses()).containsOnly("a.A");171 }172 @Test173 public void return_bootstrap_classloader() throws Exception {174 assertThat(jdkClassLoader()).isNotEqualTo(Mockito.class.getClassLoader());175 assertThat(jdkClassLoader()).isNotEqualTo(ClassLoaders.class.getClassLoader());176 assertThat(jdkClassLoader()).isEqualTo(Number.class.getClassLoader());177 assertThat(jdkClassLoader()).isEqualTo(null);178 }179 @Test180 public void return_current_classloader() throws Exception {181 assertThat(currentClassLoader()).isEqualTo(this.getClass().getClassLoader());182 }183 static class ClassUsingInterface1 implements Interface1 { }184 interface Interface1 { }185}...
InMemoryClassLoader
Using AI Code Generation
1import org.junit.Test;2import org.junit.runner.RunWith;3import org.mockito.runners.MockitoJUnitRunner;4@RunWith(MockitoJUnitRunner.class)5public class TestClass {6 public void testLoadClass() throws Exception {7 .builder(ClassLoaders.getClassLoader(getClass()), "MyClass")8 .source("MyClass", "package com.example; public class MyClass {}")9 .build()10 .loadClass("com.example.MyClass");11 System.out.println(clazz);12 }13}
InMemoryClassLoader
Using AI Code Generation
1@DisplayName("Class under test")2class ClassUnderTestTest {3 @DisplayName("should do something")4 void shouldDoSomething() {5 }6}7@DisplayName("Class under test")8class ClassUnderTestTest {9 @DisplayName("should do something")10 void shouldDoSomething() {11 }12}13@DisplayName("Class under test")14class ClassUnderTestTest {15 @DisplayName("should do something")16 void shouldDoSomething() {17 }18}19@DisplayName("Class under test")20class ClassUnderTestTest {21 @DisplayName("should do something")22 void shouldDoSomething() {23 }24}25@DisplayName("Class under test")26class ClassUnderTestTest {27 @DisplayName("should do something")28 void shouldDoSomething() {29 }30}31@DisplayName("Class under test")32class ClassUnderTestTest {33 @DisplayName("should do something")34 void shouldDoSomething() {35 }36}37@DisplayName("Class under test")38class ClassUnderTestTest {39 @DisplayName("should do something")40 void shouldDoSomething() {41 }42}
InMemoryClassLoader
Using AI Code Generation
1public class ClassToBeLoaded {2 public String hello() {3 return "Hello World";4 }5}6public class ClassLoaderTest {7 public void classLoaderTest() {8 ClassToBeLoaded classToBeLoaded = new ClassToBeLoaded();9 byte[] byteCode = ClassFileBytesExtractor.extractBytesFromClassFile(ClassToBeLoaded.class);10 ClassLoader classLoader = new InMemoryClassLoader(byteCode);11 Class<?> clazz = null;12 try {13 clazz = classLoader.loadClass(ClassToBeLoaded.class.getName());14 Object instance = clazz.newInstance();15 Method helloMethod = clazz.getDeclaredMethod("hello");16 String hello = (String) helloMethod.invoke(instance);17 System.out.println(hello);18 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {19 e.printStackTrace();20 }21 }22}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!