BAEL-870 How to Call a Method During Runtime Using Reflection (#1789)
* BAEL-870 How to Call a Method During Runtime Using Reflection * Update OperationsUnitTest.java Rename Test Name
This commit is contained in:
parent
cae4e4c78d
commit
934fc3ff39
@ -14,4 +14,8 @@ public class Operations {
|
|||||||
return a && b;
|
return a && b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int max(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,22 +15,22 @@ public class OperationsUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalAccessException.class)
|
@Test(expected = IllegalAccessException.class)
|
||||||
public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
|
public void givenObject_whenInvokePrivateMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||||
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
Method andPrivateMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||||
|
|
||||||
Operations operationsInstance = new Operations();
|
Operations operationsInstance = new Operations();
|
||||||
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
|
Boolean result = (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
|
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
|
||||||
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
Method andPrivatedMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||||
andInstanceMethod.setAccessible(true);
|
andPrivatedMethod.setAccessible(true);
|
||||||
|
|
||||||
Operations operationsInstance = new Operations();
|
Operations operationsInstance = new Operations();
|
||||||
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
|
Boolean result = (Boolean) andPrivatedMethod.invoke(operationsInstance, true, false);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.java.reflection.operations;
|
||||||
|
|
||||||
|
import com.baeldung.java.reflection.*;
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
public class MoreOperationsUnitTest {
|
||||||
|
|
||||||
|
public MoreOperationsUnitTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalAccessException.class)
|
||||||
|
public void givenObject_whenInvokeProtectedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||||
|
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class);
|
||||||
|
|
||||||
|
Operations operationsInstance = new Operations();
|
||||||
|
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
|
||||||
|
System.out.println("result = " + result);
|
||||||
|
assertThat(result, equalTo(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenObject_whenInvokeProtectedMethod_thenCorrect() throws Exception {
|
||||||
|
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class);
|
||||||
|
maxProtectedMethod.setAccessible(true);
|
||||||
|
|
||||||
|
Operations operationsInstance = new Operations();
|
||||||
|
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
|
||||||
|
|
||||||
|
assertThat(result, equalTo(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user