Setting Expectations
The Mockito-based example that mocked the PersonRepository dependency of PersonService used the methods when and thenReturn to set the expectations on the findAll method. Here’s the signature for when:
| public static <T> OngoingStubbing<T> when(T methodCall) |
The argument to when is the declaration of an invocation of the method you want to call on the stub. The return type connects to the various then methods, like thenReturn, thenThrow, or thenAnswer, which are normally chained to the output. There are a couple of overloads of the thenReturn method:
| OngoingStubbing<T> thenReturn(T value) |
| OngoingStubbing<T> thenReturn(T value, T... values) |
For example, add another method to the PersonService called findByIds, which takes ...
Get Mockito Made Clear now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.