JAVA-14232 JAVA-19121 Move article Difference Between Class.forName() and Class.forName().newInstance() to core-java-jvm-3 module (#13604)
This commit is contained in:
@@ -6,4 +6,5 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
|
||||
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
||||
- [Compiling and Executing Code From a String in Java](https://www.baeldung.com/java-string-compile-execute-code)
|
||||
- [Difference Between Class.forName() and Class.forName().newInstance()](https://www.baeldung.com/java-class-forname-vs-class-forname-newinstance)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm-2)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.loadclass;
|
||||
|
||||
|
||||
public class MyClassForLoad {
|
||||
|
||||
private String data = "some data";
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user