12
Jul

IConfigurable and IHookable example in testng

IConfigurable interface can be implemented to configure all “Configurable annotations” eg. @BeforeSuite, @BeforeTest etc.

method: run(IConfigureCallBack cb, ITestResult itr)

Following example demonstrate difference between IConfigurable and IHookable interface implementation in TestNG

Step 1: Custom implementation of above interfaces
————————————————–

public class MyConfig implements IConfigurable, IHookable
{
@Override
public void run(IConfigureCallBack cb, ITestResult itr)
{
System.out.println(“Execute this before any configuration steps gets executed”);
cb.runConfigurationMethod(itr);
}

@Override
public void run(IHookCallBack cb, ITestResult tr)
{
System.out.println(“Execute this before any Test step is executed”);
cb.runTestMethod(tr);
}
}

Step 2: your testng script:
—————————

@Listeners(MyConfig.class)
public class MyTestNGScript
{
@BeforeSuite
public void bf()
{
System.out.println(“Before suite step”);
}

@Test
public void t()
{
System.out.println(“Before @Test step”);
}
}

output:
——–