Mockito doesn't check for more interactions, but omits getters

Mockito api provides a method:

Mockito.verifyNoMoreInteractions(someMock); 

but is it possible to declare in Mockito that I don’t want more interactions with this layout with exceptions for interactions with its getter methods?

A simple scenario is one in which I test that SUT only changes certain properties of a given layout and leaves other properties unused.

In the example, I want to verify that UserActivationService modifies the Active property in an instance of the User class, but does nothing for properties such as Role, Password, AccountBalance, etc.

+7
unit-testing mockito mocking
source share
1 answer

This feature is currently not available in Mockito. If you need it, you can create it yourself using reflection wizzardry, although it will be a little painful.

My suggestion was to check the number of interactions on methods that you don't want to call 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 sensitivity of the WorldLeader API design can be controversial, but it should do as an example.

+13
source share

All Articles