好吧,我有点困惑。
public class GroupDemo {
@Test()
public void test1() {
System.out.println("test1");
}
}
和
public class GroupDemoChild extends GroupDemo{
@Test
public void atest(){
System.out.println("atest");
}
@AfterMethod
public void after() {
System.out.println("after method");
}
}
这里合乎逻辑的是:
test1
atest
after method
但是我得到:
test1
after method
atest
after method
所以 after()
被调用了两次。
如何仅在声明类的方法之后运行它?
TestNG = 6.8.5; Java = 1.7
最佳答案
您还可以利用 IInvokedMethodListener 的自定义实现界面以及一些自定义注释。像这样的东西:
@Listeners({ com.somepackage.MethodInvocationListener.class })
public class GroupDemoChild extends GroupDemo{
@Test
@AfterEd
public void atest(){
System.out.println("atest");
}
}
public class MethodInvocationListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
// doNothing
}
@Override
public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
if (iInvokedMethod.getTestMethod().getConstructorOrMethod()
.getMethod().getAnnotation(AfterEd.class) != null) {
System.out.println("after method");
}
}
}
关于java - TestNG @AfterMethod 绑定(bind)到当前类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30481940/