[英]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.