BAEL-3504: Added Unit test file and removed old class file
This commit is contained in:
parent
6f4ebbac9f
commit
82cffb4b61
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user