JUnit_InvocationTargetException_e.getCause()で詳細原因を調べる

junitテスト中に有用な情報を見つけた。

より詳しい原因がe.getCause()で取得できる。

 

Java での java.lang.reflect.InvocationTargetException エラーについて | Delft スタック

 

 

import java.lang.reflect.*;

public class Test {
  public int divideByZero() {
    return 89 / 0;
  }

  public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
    Test obj = new Test();
    Method method = Test.class.getMethod("divideByZero");
    try {
      method.invoke(obj);
    } catch (InvocationTargetException e) {
      System.out.println(e.getCause());
    }
  }
}