Argument matchers play a key role in mocking. Following are the rationale and examples of Argument matchers.
Mock objects return expected values. But when it needs to return different values for different arguments, argument matcher comes into play. Suppose we have a method that takes a player name as the input and returns the number of runs as the output. We want to stub it and return 100 for Sachin
and 10 for xyz
. We have to use argument matcher to stub this.
Mockito returns expected values when a method is stubbed. If the method takes arguments, the argument must match during the execution. For example, the getValue(int someValue)
method is stubbed in the following way:
when(mockObject.getValue(1)).thenReturn(expected value);
Here, the getValue
method is called with mockObject.getValue(100)
. Then, the parameter doesn't match (it is expected that the method will be called with 1, but at runtime it encounters 100), so the mock object fails to return the expected...