[BAEL-5788] Accessing Private Constructor
This commit is contained in:
parent
3af60557bd
commit
cb308a89e8
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.reflection;
|
||||
|
||||
public class PrivateConstructorClass {
|
||||
|
||||
private PrivateConstructorClass() {
|
||||
System.out.println("Used the private constructor!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PrivateConstructorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenConstructorIsPrivate_thenInstanceSuccess() throws Exception {
|
||||
Constructor<PrivateConstructorClass> pcc = PrivateConstructorClass.class.getDeclaredConstructor();
|
||||
pcc.setAccessible(true);
|
||||
PrivateConstructorClass privateConstructorInstance = pcc.newInstance();
|
||||
Assertions.assertTrue(privateConstructorInstance instanceof PrivateConstructorClass);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue