BAEL-3504: Added Unit test file and removed old class file

This commit is contained in:
Vikas Ramsingh Rajput 2020-01-17 23:38:53 +03:00
parent 6f4ebbac9f
commit 82cffb4b61
2 changed files with 20 additions and 19 deletions

View File

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

View File

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