From 9d9d0cc4276bab8a29c32b6bb11f8b85ed762576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Ju=C3=A1rez?= Date: Thu, 4 May 2017 22:27:05 -0500 Subject: [PATCH] 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? --- .../baeldung/java/reflection/Operations.java | 17 ++++++ .../java/reflection/OperationsUnitTest.java | 53 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/java/reflection/Operations.java create mode 100644 core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.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 new file mode 100644 index 0000000000..754a93122f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/reflection/Operations.java @@ -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; + } + +} 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 new file mode 100644 index 0000000000..da0b436275 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java @@ -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); + } + +}