views:

99

answers:

1

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 sut changes only certain properties of a given mock and lefts 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.

I'm open to criticism regarding my approach to the problem.

+1  A: 

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(1)).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