2

I want to check if any method is called on a mock, the mock contains many method and i don't want to write many call in that way:

verify(mock).method1();

verify(mock).method2();

picciano
  • 22,341
  • 9
  • 69
  • 82
Ala Messaoudi
  • 366
  • 4
  • 12
  • What you would be testing / asserting? Basically nothing. – Michael Apr 19 '18 at 13:41
  • 1
    Possible duplicate of [Mockito : how to verify method was called on an object created within a method?](https://stackoverflow.com/questions/9841623/mockito-how-to-verify-method-was-called-on-an-object-created-within-a-method) – fhe Apr 19 '18 at 13:44
  • I wouldn't recommend simply verifying that your mock object was interacted with. For a good unit test you should verify that the mock was interacted with as expected i.e. that each method on that mock was invoked with the correct arguments etc. I don't think there is a way to do this with Mockito any way which I would think is on purpose. – Garreth Golding Apr 19 '18 at 13:46
  • @ggolding is there a way to check the exception rised by Mockito.verifyZeroInteractions(mock) so i can conclude that a method was invoked on the mock? – Ala Messaoudi Apr 19 '18 at 13:51
  • I'm sure you could catch the exception in a try-catch block but in my opinion this is a very hacky way of doing it. My suggestion would be just to verify each method was interacted with separately. Even just verifying half the methods would be better than what you are trying to do. – Garreth Golding Apr 19 '18 at 13:55
  • @ggolding i have about 50 method. for the moment i'am using this hack, waiting to find a better solutionn – Ala Messaoudi Apr 19 '18 at 15:13
  • I'd recommend rewriting your method or class under test. If this method class 50 methods it sounds like possible code smell or bad design. – Garreth Golding Apr 19 '18 at 15:19

1 Answers1

3

For the moment I'am using this hack, waiting to find a better solution:

boolean isThereAnyInerraction= false;
try {
  Mockito.verifyZeroInteractions(maock);
} catch(NoInteractionsWanted e){
  isThereAnyInerraction = true;
}
assertThat(isThereAnyInerraction).isTrue();

The method verifyZeroInteractions(mock) as it's name, verifies that no method is invoked on a mock.

Ala Messaoudi
  • 366
  • 4
  • 12