Reputation: 13
I'm currently implementing a method of IRetryAnalyzer in combination with IAnnotationTransformer to perform a retry when my tests fail. I want to add a Thread.sleep() when the Retry occurs. Here's what I currently have:
public boolean retry(ITestResult result){
if (retryCount < maxRetryCount) {
retryCount++;
return true;
}
return false;
}
When I add Thread.sleep() it requires that I add an thrown Exception for the Retry method:
public boolean retry(ITestResult result) throws Exception{
if (retryCount < maxRetryCount) {
retryCount++;
Thread.sleep(5);
return true;
}
return false;
}
However, the "Exception" gives me an error: "Overriden method does not throw java.lang.Exception". It seems like I can't actually add a Sleep to this retry despite how much I've tried. Does anyone know a work around?
Upvotes: 1
Views: 1985
Reputation: 1689
There is a difference between 'throws' declaration and the 'try-catch' block. The difference is, 'try-catch' swallows the exception and 'throws' will let it propagate. Refer the below link:
try/catch versus throws Exception
As retry is an overridden method, there are some rules about method overriding with exception handling.
The rules are:
1. If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
2. If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
So, declare an exception according to the above rules or surround the 'Thread.sleep(5000)' with try-catch block like below.
try {
Thread.sleep(5000);
} catch (Exception e) {
//do nothing
}
Note:
The Thread.sleep() will actually consider value as milliseconds, so use 5000 instead of 5.
Upvotes: 0
Reputation: 3227
Thread.sleep(long)
could throw InterrupedException
.
You can include it into a try catch
block and also remove throws Exception
from method.
public boolean retry(ITestResult result){
if (retryCount < maxRetryCount) {
retryCount++;
try{
Thread.sleep(5);
}catch(Exception e){}
return true;
}
return false;
}
Upvotes: 0
Reputation: 3131
Catch and suppress the InterruptedException
that would be thrown.
try {
Thread.sleep(5);
} catch (InterruptedException e) {
//do nothing
}
This will prevent you from needing to add the throws
clause to the method definition.
Upvotes: 1