File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 22
33import static org .hamcrest .CoreMatchers .equalTo ;
44import static org .junit .Assert .assertFalse ;
5+ import static org .junit .Assert .assertTrue ;
56import static org .junit .Assert .assertThat ;
67
78import 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 {
You can’t perform that action at this time.
0 commit comments