From e19c356bf97fefea8be76e414fe01f68b56ea9e0 Mon Sep 17 00:00:00 2001 From: Vikas Ramsingh Rajput Date: Fri, 17 Jan 2020 23:38:53 +0300 Subject: [PATCH] BAEL-3504: Added Unit test file and removed old class file --- .../InvocationTargetDemo.java | 19 ------------------ .../InvocationTargetUnitTest.java | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 19 deletions(-) delete mode 100644 core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetDemo.java create mode 100644 core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetUnitTest.java diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetDemo.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetDemo.java deleted file mode 100644 index 125a5d649f..0000000000 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetDemo.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.reflection.exception.invocationtarget; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -public class InvocationTargetDemo { - public static void main(String[] args) throws Throwable { - - try { - - InvocationTargetExample targetExample = new InvocationTargetExample(); - Method method = InvocationTargetExample.class.getMethod("divideByZeroExample"); - method.invoke(targetExample); - } catch (InvocationTargetException e) { - - throw e.getCause(); - } - } -} diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetUnitTest.java new file mode 100644 index 0000000000..938dff87d4 --- /dev/null +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/exception/invocationtarget/InvocationTargetUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.reflection.exception.invocationtarget; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.jupiter.api.Test; + +public class InvocationTargetUnitTest { + + @Test + public void whenCallingMethodThrowsException_thenAssertTrue() throws Exception { + InvocationTargetExample targetExample = new InvocationTargetExample(); + Method method = InvocationTargetExample.class.getMethod("divideByZeroExample"); + Exception exception = assertThrows(InvocationTargetException.class, () -> method.invoke(targetExample)); + assertEquals(ArithmeticException.class, exception.getCause().getClass()); + } +}