代码示例:
Test1类将是一个负责执行Test2类方法的父类
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
import 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代码:
import org.testng.Assert;
public class Test2 {
public void test(String obj){
Assert.assertEquals(obj, "1");
}
} 在这个示例中,断言应该失败,但是testng报告状态为已通过 那么我如何将断言失败链接到testng报告
答案 0 :(得分:0)
我认为最好在单个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]>