2015-11-05 130 views
0

好吧,所以我顯然不太明白doReturn(...)。(...)和when(...)。thenReturn(...)之間的區別。DoReturn拋出UnfinishedStubbingException

事情是當我有一個類,我用@Mock註釋Mocks並在我的類中用@InjectMocks注入它們時我想測試,然後在這些模擬對象之一上使用doReturn,我得到一個UnfinishedStubbingException。但是當我使用時(...),然後返回(...)一切似乎工作得很好。我認爲doReturn是更好的建議,因爲它並沒有真正調用這個方法(我猜(...)。然後返回(...)不會,因爲這個字段是模擬的。)

所以這裏有一個例子:

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

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

回答

10

正確的調用是:

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

而且不.when(whatever.theMethod())

相關問題