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:

JavaScript

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

JavaScript

Or you can verify that no other method was called:

JavaScript

For more information see the Mockito documentation.

Advertisement