When I give the retry method to some selected test cases, all such test cases will retry for the given amount of times, but I want to retry test cases only if there is a NUll Point exception, is there any way to do it with testNG?
Asked
Active
Viewed 71 times
0
-
You should aim for deterministic tests. – Peter Csala Sep 21 '22 at 14:05
1 Answers
0
Define RetryAnalyzer
where you would check for Throwable
actual type like:
public class MyRetry implements IRetryAnalyzer {
private int retryCount = 0;
private static final int maxRetryCount = 3;
@Override
public boolean retry(ITestResult result) {
if(result.getThrowable() instanceof NullPointerException
&& retryCount < maxRetryCount){
retryCount++;
return true;
}
return false;
}
}
And then use it like:
@Test(retryAnalyzer = MyRetry.class)
public class MyTest {
Object o;
public void testRetryNPE(){
o.toString();
}
public void testNoNPE(){
int i = 1/0;
}
}

Alexey R.
- 8,057
- 2
- 11
- 27