O'Reilly logo

Test-Driven Development with Mockito by Sujoy Acharya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The argument matcher

Argument matchers play a key role in mocking. Following are the rationale and examples of Argument matchers.

Rationale

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required