lundi 8 juin 2015

Can Mockito verify total number of method calls on a mocked Object?

Mockito's verify can assert a certain number of interactions with a method on a mocked object occurred.

// Given
SomeService someService = mock(SomeService.class);

// When
someService.prepare();
someService.prepare();

// Then
verify(someService, times(2)).prepare(); // test passes

Sometimes it is useful in unit tests to know that the total number of method invocations on a mocked object has not changed.

This provides visibility (i.e. a failing test) when new method invocations are added.

Does Mockito provide this functionality?

In certain situations I'd want to call:

verify(someService, times(2));

..without getting an UnfinishedVerificationException:

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock)...

Example of correct verification:
  verify(mock).doSomething()

Aucun commentaire:

Enregistrer un commentaire