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
@ -1,17 +1,21 @@
|
|||||||
package com.baeldung.java.reflection;
|
package com.baeldung.java.reflection;
|
||||||
|
|
||||||
public class Operations {
|
public class Operations {
|
||||||
|
|
||||||
public double sum(int a, double b) {
|
public double sum(int a, double b) {
|
||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double multiply(float a, long b){
|
public static double multiply(float a, long b) {
|
||||||
return a * b;
|
return a * b;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean and(boolean a, boolean b) {
|
private boolean and(boolean a, boolean b) {
|
||||||
return a && b;
|
return a && b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int max(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,23 +14,23 @@ public class OperationsUnitTest {
|
|||||||
public OperationsUnitTest() {
|
public 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);
|
||||||
}
|
}
|
||||||
@ -40,17 +40,17 @@ public class OperationsUnitTest {
|
|||||||
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class);
|
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class);
|
||||||
|
|
||||||
Operations operationsInstance = new Operations();
|
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));
|
assertThat(result, equalTo(4.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception {
|
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));
|
assertThat(result, equalTo(7.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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