Skip to content

Commit 934fc3f

Browse files
juarezjaramilloKevinGilmore
authored andcommitted
BAEL-870 How to Call a Method During Runtime Using Reflection (eugenp#1789)
* BAEL-870 How to Call a Method During Runtime Using Reflection * Update OperationsUnitTest.java Rename Test Name
1 parent cae4e4c commit 934fc3f

3 files changed

Lines changed: 59 additions & 17 deletions

File tree

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package com.baeldung.java.reflection;
22

33
public class Operations {
4-
4+
55
public double sum(int a, double b) {
66
return a + b;
77
}
8-
9-
public static double multiply(float a, long b){
8+
9+
public static double multiply(float a, long b) {
1010
return a * b;
1111
}
1212

1313
private boolean and(boolean a, boolean b) {
1414
return a && b;
1515
}
16-
16+
17+
protected int max(int a, int b) {
18+
return a > b ? a : b;
19+
}
20+
1721
}

‎core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ public class OperationsUnitTest {
1414
public OperationsUnitTest() {
1515
}
1616

17-
@Test(expected=IllegalAccessException.class)
18-
public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
19-
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
17+
@Test(expected = IllegalAccessException.class)
18+
public void givenObject_whenInvokePrivateMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
19+
Method andPrivateMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
2020

2121
Operations operationsInstance = new Operations();
22-
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
22+
Boolean result = (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
2323

2424
assertFalse(result);
2525
}
2626

2727
@Test
2828
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
29-
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
30-
andInstanceMethod.setAccessible(true);
29+
Method andPrivatedMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
30+
andPrivatedMethod.setAccessible(true);
3131

3232
Operations operationsInstance = new Operations();
33-
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
33+
Boolean result = (Boolean) andPrivatedMethod.invoke(operationsInstance, true, false);
3434

3535
assertFalse(result);
3636
}
@@ -40,17 +40,17 @@ public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {
4040
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class);
4141

4242
Operations operationsInstance = new Operations();
43-
Double result = (Double)sumInstanceMethod.invoke(operationsInstance, 1, 3);
44-
43+
Double result = (Double) sumInstanceMethod.invoke(operationsInstance, 1, 3);
44+
4545
assertThat(result, equalTo(4.0));
4646
}
47-
47+
4848
@Test
4949
public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception {
50-
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply",float.class, long.class);
50+
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply", float.class, long.class);
51+
52+
Double result = (Double) multiplyStaticMethod.invoke(null, 3.5f, 2);
5153

52-
Double result = (Double)multiplyStaticMethod.invoke(null, 3.5f, 2);
53-
5454
assertThat(result, equalTo(7.0));
5555
}
5656

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.java.reflection.operations;
2+
3+
import com.baeldung.java.reflection.*;
4+
import static org.hamcrest.CoreMatchers.equalTo;
5+
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
9+
import org.junit.Test;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class MoreOperationsUnitTest {
13+
14+
public MoreOperationsUnitTest() {
15+
}
16+
17+
@Test(expected = IllegalAccessException.class)
18+
public void givenObject_whenInvokeProtectedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
19+
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class);
20+
21+
Operations operationsInstance = new Operations();
22+
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
23+
System.out.println("result = " + result);
24+
assertThat(result, equalTo(4));
25+
}
26+
27+
@Test
28+
public void givenObject_whenInvokeProtectedMethod_thenCorrect() throws Exception {
29+
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class);
30+
maxProtectedMethod.setAccessible(true);
31+
32+
Operations operationsInstance = new Operations();
33+
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
34+
35+
assertThat(result, equalTo(4));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)