From 934fc3ff396f10f92320a7f9759e1d2aeb79a0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Ju=C3=A1rez?= Date: Sat, 6 May 2017 13:00:40 -0500 Subject: [PATCH] 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 --- .../baeldung/java/reflection/Operations.java | 12 ++++-- .../java/reflection/OperationsUnitTest.java | 26 ++++++------- .../operations/MoreOperationsUnitTest.java | 38 +++++++++++++++++++ 3 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 core-java/src/test/java/com/baeldung/java/reflection/operations/MoreOperationsUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/java/reflection/Operations.java b/core-java/src/main/java/com/baeldung/java/reflection/Operations.java index 754a93122f..dd11a9bcee 100644 --- a/core-java/src/main/java/com/baeldung/java/reflection/Operations.java +++ b/core-java/src/main/java/com/baeldung/java/reflection/Operations.java @@ -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; + } + } diff --git a/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java index 4bd12047c6..71ee466afd 100644 --- a/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java @@ -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)); } diff --git a/core-java/src/test/java/com/baeldung/java/reflection/operations/MoreOperationsUnitTest.java b/core-java/src/test/java/com/baeldung/java/reflection/operations/MoreOperationsUnitTest.java new file mode 100644 index 0000000000..a7b5df01dd --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/reflection/operations/MoreOperationsUnitTest.java @@ -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)); + } + +}