mgamer
mgamer

Reputation: 14060

Mockito verify no more interactions but omit getters

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.

Upvotes: 8

Views: 11433

Answers (1)

iwein
iwein

Reputation: 26161

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.

Upvotes: 16

Related Questions