Skip to content

Commit 081b3b8

Browse files
authored
Merge pull request eugenp#8485 from vikasrajput6035/BAEL-3504
BAEL-3504: Article - what causes invocation-target-exception : done
2 parents 5e13850 + ca464ab commit 081b3b8

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.reflection.exception.invocationtarget;
2+
3+
public class InvocationTargetExample {
4+
public int divideByZeroExample() {
5+
return 1 / 0;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.reflection.exception.invocationtarget;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
public class InvocationTargetUnitTest {
12+
13+
@Test
14+
public void whenCallingMethodThrowsException_thenAssertCauseOfInvocationTargetException() throws Exception {
15+
16+
InvocationTargetExample targetExample = new InvocationTargetExample();
17+
Method method = InvocationTargetExample.class.getMethod("divideByZeroExample");
18+
19+
Exception exception = assertThrows(InvocationTargetException.class, () -> method.invoke(targetExample));
20+
21+
assertEquals(ArithmeticException.class, exception.getCause().getClass());
22+
}
23+
}

0 commit comments

Comments
 (0)