The best practice of TestNg behavior overriding is using so called listeners. For example you can override the dafault behavior of what is happenning when your assert fails. To do that you need to extend TestListenerAdapter
class as it is shown below:
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class StringComparatorListener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult iTestResult) {
System.out.println("Not Equal");
super.onTestFailure(iTestResult);
}
@Override
public void onTestSuccess(ITestResult iTestResult) {
System.out.println("Equal");
super.onTestSuccess(iTestResult);
}
}
Now you should add the listener to your test. Either by configuring testng.xml
or by adding listener straigt in the code. Below is the example of how to add the above listener to your code:
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
@Listeners(test.StringComparatorListener.class)
public class TestChangeTestNgBehavior {
@Test
public void testAdd() {
String str = "One string";
assertEquals("Other string", str);
}
}
This is the minimal and complete example which you can run and test and amend according to your particular need.