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:
Raúl Juárez 2017-05-06 13:00:40 -05:00 committed by KevinGilmore
parent cae4e4c78d
commit 934fc3ff39
3 changed files with 59 additions and 17 deletions

View File

@ -1,17 +1,21 @@
package com.baeldung.java.reflection;
public class Operations {
public double sum(int a, double b) {
return a + b;
}
public static double multiply(float a, long b){
public static double multiply(float a, long b) {
return a * b;
}
private boolean and(boolean a, boolean b) {
return a && b;
}
protected int max(int a, int b) {
return a > b ? a : b;
}
}

View File

@ -14,23 +14,23 @@ public class OperationsUnitTest {
public 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);
@Test(expected = IllegalAccessException.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);
}
@ -40,17 +40,17 @@ public class OperationsUnitTest {
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class);
Operations operationsInstance = new Operations();
Double result = (Double)sumInstanceMethod.invoke(operationsInstance, 1, 3);
Double result = (Double) sumInstanceMethod.invoke(operationsInstance, 1, 3);
assertThat(result, equalTo(4.0));
}
@Test
public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception {
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply",float.class, long.class);
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply", float.class, long.class);
Double result = (Double) multiplyStaticMethod.invoke(null, 3.5f, 2);
Double result = (Double)multiplyStaticMethod.invoke(null, 3.5f, 2);
assertThat(result, equalTo(7.0));
}

View File

@ -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));
}
}