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;
|
||||
}
|
||||
|
||||
protected int max(int a, int b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,22 +15,22 @@ public class OperationsUnitTest {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalAccessException.class)
|
||||
public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
|
||||
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||
public void givenObject_whenInvokePrivateMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||
Method andPrivateMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||
|
||||
Operations operationsInstance = new Operations();
|
||||
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
|
||||
Boolean result = (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
|
||||
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||
andInstanceMethod.setAccessible(true);
|
||||
Method andPrivatedMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||
andPrivatedMethod.setAccessible(true);
|
||||
|
||||
Operations operationsInstance = new Operations();
|
||||
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
|
||||
Boolean result = (Boolean) andPrivatedMethod.invoke(operationsInstance, true, false);
|
||||
|
||||
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