[英]Mockito SpyBean throws an UnfinishedStubbingException
[英]DoReturn throws UnfinishedStubbingException
好的,所以我顯然不太了解doReturn(...)。when(...)和when(...)。thenReturn(...)之間的區別。
當我有一個類,我用@Mock注釋Mocks並在我的類中注入@InjectMocks我想要測試然后在其中一個模擬中使用doReturn我得到一個UnfinishedStubbingException。 但是當我使用的時候(...)。然后返回(...)一切似乎都運行得很好。 我認為doReturn更值得推薦,因為它並沒有真正調用該方法(我猜這是(...)。thenReturn(...)不會,因為該字段是模擬。)
所以這是一個例子:
doReturn(siteModel).when(siteService.getCurrentSite()) --> UnfinishedStubbingException
when(siteService.getCurrentSite()).thenReturn(siteModel) --> Works just fine
正確的調用是:
doReturn(...).when(whatever).theMethod()
而不是.when(whatever.theMethod())
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.