I’m new to Mockito and have a question.
Say I have a method:
SomeClass someMethod() throws SomeException{};
and, its called in a loop:
for(int i=0; i < 2; 1++) {
try {
SomeClass someObject = someMethod();
} catch(SomeException e) {
//. Do something
}
}
When mocking, I know we can separately mock the execution of the method for a call that returns a SomeClass object successfully, as well as mock an exception scenario:
Successful result:
when(someMethod()).thenReturn(new SomeClass());
Exception:
when(someMethod()).thenThrow(new SomeException());
My questions is:
How would I mock the someMethod() method call, so that on one invocation it successfully returns a SomeClass object while on the other invocation it, throws a SomeException?
Basically, in order to get code coverage for both successful and exception scenarios in:
for(int i=0; i < 2; 1++) {
try {
SomeClass someObject = someMethod();
} catch(SomeException e) {
// Do something
}
}
I hope this makes sense! Any help in this regard would be greatly appreciated.
Thanks in advance!