Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 889
user5417542 Avatar asked Nov 05 '15 09:11

user5417542


1 Answers

The correct invocation is:

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

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

like image 99
fge Avatar answered Sep 30 '22 19:09

fge