BAEL-870 How to call a method during runtime using reflection? (#1769)

* BAEL 870 How to call method during runtime using reflection?

* BAEL-870 How to call a method during runtime using reflection?
This commit is contained in:
Raúl Juárez 2017-05-04 22:27:05 -05:00 committed by KevinGilmore
parent 14f90c8636
commit 9d9d0cc427
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,17 @@
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){
return a * b;
}
private boolean and(boolean a, boolean b) {
return a && b;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.java.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.testng.Assert;
import org.testng.annotations.Test;
public class OperationsUnitTest {
public OperationsUnitTest() {
}
@Test(expectedExceptions = IllegalAccessException.class)
public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
Operations operationsInstance = new Operations();
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
Assert.assertFalse(result);
}
@Test
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
andInstanceMethod.setAccessible(true);
Operations operationsInstance = new Operations();
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
Assert.assertFalse(result);
}
@Test
public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class);
Operations operationsInstance = new Operations();
Double result = (Double)sumInstanceMethod.invoke(operationsInstance, 1, 3);
Assert.assertTrue(4 == result);
}
@Test
public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception {
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply",float.class, long.class);
Double result = (Double)multiplyStaticMethod.invoke(null, 3.5f, 2);
Assert.assertTrue(7 == result);
}
}