diff --git a/core-java-modules/core-java/src/main/java/com/baeldung/loadclass/MyClassForLoad.java b/core-java-modules/core-java/src/main/java/com/baeldung/loadclass/MyClassForLoad.java new file mode 100644 index 0000000000..e97848fb1f --- /dev/null +++ b/core-java-modules/core-java/src/main/java/com/baeldung/loadclass/MyClassForLoad.java @@ -0,0 +1,8 @@ +package com.baeldung.loadclass; + + +public class MyClassForLoad { + + private String data = "some data"; + +} diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/loadclass/LoadClassUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/loadclass/LoadClassUnitTest.java new file mode 100644 index 0000000000..1f7eb139a2 --- /dev/null +++ b/core-java-modules/core-java/src/test/java/com/baeldung/loadclass/LoadClassUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.loadclass; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.InvocationTargetException; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + + +public class LoadClassUnitTest { + + @Test + public void whenUseClassForName_createdInstanceOfClassClass() throws ClassNotFoundException { + Class instance = Class.forName("com.baeldung.loadclass.MyClassForLoad"); + assertInstanceOf(Class.class, instance, "instance should be of Class.class"); + } + + @Test + public void whenUseClassForNameWithNewInstance_createdInstanceOfTargetClas() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + Object instance = Class.forName("com.baeldung.loadclass.MyClassForLoad").newInstance(); + assertInstanceOf(MyClassForLoad.class, instance, "instance should be of MyClassForLoad class"); + } + + @Test + public void whenUseClassForNameWithDeclaredConstructor_newInstanceCreatedInstanceOfTargetClas() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + Object instance = Class.forName("com.baeldung.loadclass.MyClassForLoad").getDeclaredConstructor().newInstance(); + assertInstanceOf(MyClassForLoad.class, instance, "instance should be of MyClassForLoad class"); + } + +}