Mockito验证没有更多的交互,但省略了getter

mga*_*mer 8 unit-testing mocking mockito

Mockito api提供的方法:

Mockito.verifyNoMoreInteractions(someMock);
Run Code Online (Sandbox Code Playgroud)

但是在Mockito中是否有可能声明我不希望与给定模拟器进行更多交互,除了与其getter方法的交互之外?

简单的场景是我测试SUT只更改给定模拟的某些属性并使其他属性未被打开的情况.

在示例中,我想测试UserActivationService在类User的实例上更改属性Active但不对Role,Password,AccountBalance等属性执行任何操作.

iwe*_*ein 15

目前没有此功能在Mockito中.如果你经常需要它,你可以使用反射wizzard自己创建它,虽然这会有点痛苦.

我的建议是使用以下方法验证您不希望经常调用的方法的交互次数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();
}
Run Code Online (Sandbox Code Playgroud)

当然,WorldLeader API设计的敏感性可能是有争议的,但作为一个例子,它应该做.