first commit (#11716)

This commit is contained in:
Azhwani 2022-01-21 02:27:09 +01:00 committed by GitHub
parent 112f771e53
commit 02e4cf9f99
1 changed files with 20 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.reflection;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -35,6 +36,25 @@ public class OperationsUnitTest {
assertFalse(result); assertFalse(result);
} }
@Test
public void givenObject_whenInvokePrivateMethod_thenCheckAccess() throws Exception {
Operations operationsInstance = new Operations();
Method andPrivatedMethod = Operations.class.getDeclaredMethod("privateAnd", boolean.class, boolean.class);
boolean isAccessEnabled = andPrivatedMethod.canAccess(operationsInstance);
assertFalse(isAccessEnabled);
}
@Test
public void givenObject_whenInvokePublicMethod_thenEnableAccess() throws Exception {
Operations operationsInstance = new Operations();
Method andPrivatedMethod = Operations.class.getDeclaredMethod("privateAnd", boolean.class, boolean.class);
andPrivatedMethod.trySetAccessible();
boolean isAccessEnabled = andPrivatedMethod.canAccess(operationsInstance);
assertTrue(isAccessEnabled);
}
@Test @Test
public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception { public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {
Method sumInstanceMethod = Operations.class.getMethod("publicSum", int.class, double.class); Method sumInstanceMethod = Operations.class.getMethod("publicSum", int.class, double.class);