method反射調(diào)用后拋出InvocationTargetException異常

背景

在構(gòu)建關(guān)鍵字驅(qū)動(dòng)測(cè)試框架的時(shí)候,明明已經(jīng)在在方法try/catch并且拋出了自定義異常,但是測(cè)試報(bào)告中打印錯(cuò)誤信息時(shí)卻是InvocationTargetException。原因是:如果方法中直接拋出異常,通過反射進(jìn)行調(diào)用時(shí),會(huì)拋出InvocationTargetException異常

問題場(chǎng)景

  • 自定義的某個(gè)關(guān)鍵字方法:(此例中屬于KeyWordsActions類)
public static void verifyTitle(String text) throws Exception {
        try {
            /*自定義代碼*/
        }catch (Exception e){
            throw new Exception("My Exception");

        }
  • 調(diào)用
public static void main(String args[]) throws Exception{
      KeyWords keyWords = new KeyWords();
      Method[]  method = keyWords.getClass().getMethods();
        for(int i=0;i<method.length;i++){
              if("verifyTitle".equals(method[i].getName())){ 
                try {
                    method[i].invoke(keyWords,value);//value是測(cè)試數(shù)據(jù)
                    Log.info("Pass");
                }catch(Exception e){
                    Log.error("Failed");
                    e.printStackTrace();
                }
                break;
            }
        }
    }
  • 錯(cuò)誤日志
java.lang.reflect.InvocationTargetException
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.lang.Exception: My Exception
    at com.wjn.configuration.KeyWordsActions.waitElement(KeyWordsActions.java:33)
    ... 30 more

解決方案

通過異常日志看到最終拋出的是java.lang.reflect.InvocationTargetException異常。Caused by自定義異常.
查看源代碼如下

**
* @exception InvocationTargetException if the underlying method throws an exception.
*/
@CallerSensitive
    public Object invoke(Object obj, Object... args)
        throws IllegalAccessException, IllegalArgumentException,
           InvocationTargetException
    {
        if (!override) {
            if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                Class<?> caller = Reflection.getCallerClass();
                checkAccess(caller, clazz, obj, modifiers);
            }
        }
        MethodAccessor ma = methodAccessor;             // read volatile
        if (ma == null) {
            ma = acquireMethodAccessor();
        }
        return ma.invoke(obj, args);
    }

可以通過e.getCause().toString()打印出自定義日志信息

public static void main(String args[]) throws Exception{
      KeyWords keyWords = new KeyWords();
      Method[]  method = keyWords.getClass().getMethods();
        for(int i=0;i<method.length;i++){
              if("verifyTitle".equals(method[i].getName())){ 
                try {
                    method[i].invoke(keyWords,value);//value是測(cè)試數(shù)據(jù)
                    Log.info("Pass");
                }catch(Exception e){
                    Log.error("Failed");
                    System.out.println(e.getCause().toString());
                }
                break;
            }
        }
    }

此時(shí)打印的信息就是自定義異常的文本

參考鏈接
[1] method.invoke()拋出InvocationTargetException異常

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容