Merge pull request #8485 from vikasrajput6035/BAEL-3504

BAEL-3504: Article - what causes invocation-target-exception : done
This commit is contained in:
Greg 2020-01-19 13:01:46 -05:00 committed by GitHub
commit b7c1eea772
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.reflection.exception.invocationtarget;
public class InvocationTargetExample {
public int divideByZeroExample() {
return 1 / 0;
}
}

View File

@ -0,0 +1,23 @@
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_thenAssertCauseOfInvocationTargetException() 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());
}
}