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()); + } +}