Frank Kusters
Frank Kusters

Reputation: 2634

How do I verify that, from a certain point on, there are no more interactions with a mock?

I'm trying to verify that a call to a function does not cause any interactions with a mock. However, that mock is used before calling the function, in the class' constructor.

This doesn't work, because there are interactions with the mock:

SomeMock someMock = mock(SomeMock.class);
Subject subject = new Subject(someMock); // interactions with someMock happen here

subject.doNothingWithMock();

verifyNoMoreInteractions(someMock);

This is a brittle solution:

SomeMock someMock = mock(SomeMock.class);
Subject subject = new Subject(someMock); // interactions with someMock happen here

verify(someMock).anInteraction();
verify(someMock).anotherInteraction();

subject.doNothingWithMock();

verifyNoMoreInteractions(someMock);

It's brittle because if the constructor changes to have other interactions with the mock, the test needs to be changed, even though the test doesn't test the constructor.

Is there an alternative?

Upvotes: 2

Views: 248

Answers (1)

luboskrnac
luboskrnac

Reputation: 24571

You can reset mock: Mockito.reset(someMock); or just reset(someMock); when Mockito is statically imported.

This method takes variable number of arguments so you can even do reset(someMock1, someMock2, ...);

Upvotes: 2

Related Questions