Merge pull request #13475 from StefSC/BAEL-5788-Private-Constructor
[BAEL-5788] Accessing Private Constructor
This commit is contained in:
commit
db2d8069e3
|
@ -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