From 02e4cf9f99ddfb86ed2771bd277ec205c488c8f9 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Fri, 21 Jan 2022 02:27:09 +0100 Subject: [PATCH] first commit (#11716) --- .../reflection/OperationsUnitTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java index 7584d5da94..92f5b83f2b 100644 --- a/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java +++ b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java @@ -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 {