I have a piece of code that retries when it fails with ExceptionOne
, and if it continues to fail, it will throw a ExceptionTwo
. I want to test this behavior, but am not sure how to.
public void someMethod(String x) {
boolean retry;
int try = 0;
do {
retry = false;
try {
someFunction(x);
} catch (ExceptionOne e) {
if (try < 5) {
retry = true;
attempt++;
} else {
throw new ExceptionTwo();
}
}
} while (retry);
}
private void someFunction(String x) {
doSomething(); //This can throw ExceptionOne
}
Suppose these functions are inside a SomeClass
. I mocked SomeClass
and tried something like
doThrow(new ExceptionOne()).doNothing().when(mockObject).someMethod(any())
for consecutive calls, but this isn't quite what I am looking for since I believe this is really testing the someMethod, not someFunction
or the retry functionality.
How do you test something like this? That is, how do I verify that the behavior that when someFunction
fails with ExceptionOne, it will retry until it succeeds or until it runs out of retries and throws an ExceptionTwo?