コードサンプル:
Test1 クラスは、Test2 クラス メソッドの実行を担当する親クラスになります。
import java.lang.reflect.InvocationTargetException; import 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");
}
ここでは、この例のように、アサーションは失敗するはずですが、テストレポートは合格として報告されています。