Reputation: 4972
I have the following method outside the test method
private DynamicBuild getSkippedBuild() {
DynamicBuild build = mock(DynamicBuild.class);
when(build.isSkipped()).thenReturn(true);
return build;
}
but when I call this method I get the following error
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at LINE BEING CALLED FROM
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
Looks like mockito is not happy when you stub outside the test method. Is that not supported ?
EDIT: I can get this to work by doing the stubbing in @Test
method but I want to reuse the stubbing across @Test
s.
Upvotes: 9
Views: 11254
Reputation: 242686
If isSkipped()
is not a final
method, this problem probably indicates that you try to stub a method while stubbing of another method is in progress. It's not supported because Mockito relies on order of method invocations (when()
, etc) in its stubbing API.
I guess you have something like this in your test method:
when(...).thenReturn(getSkippedBuild());
If so, you need to rewrite it as follows:
DynamicBuild build = getSkippedBuild();
when(...).thenReturn(build);
Upvotes: 15