Skip to content
Advertisement

Creating strict mock when using @MockBean of spring boot?

I use @MockBean of spring boot (with @RunWith(SpringRunner.class)) and everything was ok so far.
However the mock provides default implementation for every method of the mocked class so I cannot check if only those methods were called that I expected to be called, i.e. I would like to create strict mock.
Is that possible with @MockBean?

I don’t insist on creating strict mocks if somebody has a good idea how to check if only those methods were called that I expected.

Thanks for the help in advance!

Regards,
V.

Advertisement

Answer

With Mockito you can verify that a method was called:

verify(mockOne).add("one");

or that it was never called (never() is more readable alias for times(0)):

verify(mockOne, never()).remove("two");

Or you can verify that no other method was called:

verify(mockOne).add("one"); // check this one
verifyNoMoreInteractions(mockOne); // and nothing else

For more information see the Mockito documentation.

Advertisement