Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 921
mgamer Avatar asked May 27 '10 11:05

mgamer


1 Answers

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.

like image 69
iwein Avatar answered Sep 23 '22 23:09

iwein