8

Mockito api provides method:

Mockito.verifyNoMoreInteractions(someMock);

but is it possible in Mockito to declare that I don't want more interactions with a given mock with the exceptions of interactions with its getter methods?

The simple scenario is the one in which I test that the SUT changes only certain properties of a given mock and leaves other properties untapped.

In example I want to test that UserActivationService changes property Active on an instance of class User but does't do anything to properties like Role, Password, AccountBalance, etc.

iwein
  • 25,788
  • 10
  • 70
  • 111
mgamer
  • 13,580
  • 25
  • 87
  • 145
  • See also http://stackoverflow.com/questions/12013138/mockito-verify-no-more-interactions-with-any-mock – Vadzim Oct 17 '13 at 10:31

1 Answers1

16

No this functionality is not currently in Mockito. If you need it often you can create it yourself using reflection wizzardry although that's going to be a bit painful.

My suggestion would be to verify the number of interactions on the methods you don't want called too often using 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();
}

Of course the sensibility of the WorldLeader API design might be debatable, but as an example it should do.

iwein
  • 25,788
  • 10
  • 70
  • 111