簡體   English   中英

我在testng方法中有一個testng類,通過反射調用另一個方法無法登錄testng報告

[英]I have a testng class inside testng method , invoking another method via reflection it fails to log in testng report

代碼示例:

Test1類將是一個父類,它將負責執行Test2類的方法

導入java.lang.reflect.InvocationTargetException; 導入java.lang.reflect.Method;

導入org.testng.annotations.Test;

公共課程Test1 {

@Test
public void test() {
    Class test2 = Test2.class;
    Method method = null;
    Object obj = null;
    try {
        method = test2.getMethod("test", String.class);
    } catch (NoSuchMethodException | SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    method.setAccessible(true);

    try {
        obj = method.invoke(test2.newInstance(), "0");

    } catch (IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | InstantiationException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    }

}

}

Test2.class代碼:

導入org.testng.Assert;

公共課程Test2 {

public void test(String obj){

    Assert.assertEquals(obj, "1");
}

}在此示例中,斷言應該失敗,但是testng報告狀態為已通過那么我如何將斷言失敗鏈接到testng報告

我認為最好在單個catch塊中隔離InvocationTargetException的處理程序,並使用getCause()檢查結果。 這是我嘗試過的;

try {
    Object obj = method.invoke(test2obj, "0");
} catch( InvocationTargetException e){
    Throwable e1 = e.getCause();
    System.out.println("Method invocation failed... due to " + e1); 
} catch (IllegalAccessException | IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我得到了這個信息;

Method invocation failed... due to org.junit.ComparisonFailure: expected:<[0]> but was:<[1]>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM