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.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import java.lang.reflect.InvocationTargetException;
@ -34,6 +35,25 @@ public class OperationsUnitTest {
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
public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {