user5417542
user5417542

Reputation: 3376

DoReturn throws UnfinishedStubbingException

Okay so I obviously didn't quite understood the difference between doReturn(...).when(...) and when(...).thenReturn(...) .

The thing is I when I have a class, where I annotate Mocks with @Mock and inject them with @InjectMocks in my class I want to test and I then use doReturn on one of these mocks I get an UnfinishedStubbingException. But when I use when(...).thenReturn(...) everything seems to work just fine. I thought doReturn is more advised, since it doesn't really call the method (which I guess when(...).thenReturn(...) doesn't to, since the field is a Mock.)

So here's an example:

doReturn(siteModel).when(siteService.getCurrentSite()) --> UnfinishedStubbingException

when(siteService.getCurrentSite()).thenReturn(siteModel) --> Works just fine

Upvotes: 6

Views: 6186

Answers (1)

fge
fge

Reputation: 121720

The correct invocation is:

doReturn(...).when(whatever).theMethod()

And not .when(whatever.theMethod()).

Upvotes: 24

Related Questions