Skip to content

Commit 02e4cf9

Browse files
authored
first commit (eugenp#11716)
1 parent 112f771 commit 02e4cf9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.hamcrest.CoreMatchers.equalTo;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.assertThat;
67

78
import java.lang.reflect.InvocationTargetException;
@@ -34,6 +35,25 @@ public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
3435

3536
assertFalse(result);
3637
}
38+
39+
@Test
40+
public void givenObject_whenInvokePrivateMethod_thenCheckAccess() throws Exception {
41+
Operations operationsInstance = new Operations();
42+
Method andPrivatedMethod = Operations.class.getDeclaredMethod("privateAnd", boolean.class, boolean.class);
43+
boolean isAccessEnabled = andPrivatedMethod.canAccess(operationsInstance);
44+
45+
assertFalse(isAccessEnabled);
46+
}
47+
48+
@Test
49+
public void givenObject_whenInvokePublicMethod_thenEnableAccess() throws Exception {
50+
Operations operationsInstance = new Operations();
51+
Method andPrivatedMethod = Operations.class.getDeclaredMethod("privateAnd", boolean.class, boolean.class);
52+
andPrivatedMethod.trySetAccessible();
53+
boolean isAccessEnabled = andPrivatedMethod.canAccess(operationsInstance);
54+
55+
assertTrue(isAccessEnabled);
56+
}
3757

3858
@Test
3959
public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {

0 commit comments

Comments
 (0)