2010-05-27 34 views
7

API的Mockito提供方法:驗證的Mockito沒有更多的互動,但省略干將

Mockito.verifyNoMoreInteractions(someMock); 

但有可能在對的Mockito聲明,我不想讓更多的交互與互動與例外的給定模擬其getter方法?

這個簡單的場景是我測試SUT只改變給定模擬的某些屬性並使其他屬性未被使用的情況。

在例子中,我想測試UserActivationService改變性質活動的用戶類的一個實例,但簡化版,做任何事來樣作用,密碼,AccountBalance等特性

+0

參見http://stackoverflow.com/questions/12013138/mockito-verify-no-more-interactions-with-any-mock – Vadzim 2013-10-17 10:31:44

回答

13

沒有此功能目前不在的Mockito 。如果你經常需要它,你可以使用反射wizzardry自己創建它,雖然這會有點痛苦。

我的建議是,以驗證過於頻繁使用VerificationMode在你不想調用的方法互動次數:

@Test 
public void worldLeaderShouldNotDestroyWorldWhenMakingThreats() { 
    new WorldLeader(nuke).makeThreats(); 

    //prevent leaving nuke in armed state 
    verify(nuke, times(2)).flipArmSwitch(); 
    assertThat(nuke.isDisarmed(), is(true)); 
    //prevent total annihilation 
    verify(nuke, never()).destroyWorld(); 
} 

當然的WorldLeader API設計的感性可能會引起爭議,但作爲它應該做的一個例子。