0PricingLogin
Testing Mastery: JUnit, Mockito & Integration Tests · Lesson

Mockito Argument Matchers

Utilize argument matchers to make your verifications and stubbings more flexible and robust.

Flexible Matching with Mockito

When writing unit tests with Mockito, you often need to verify method calls or configure mock behavior based on the arguments passed to them. Sometimes, an exact match for every argument isn't flexible enough.

Argument matchers are special placeholders that allow you to specify criteria for arguments instead of their precise values, making your tests more robust and less brittle.

Why Exact Matches Fall Short

By default, Mockito uses the equals() method to compare arguments. This works perfectly for simple types like numbers or strings, and for objects that correctly override equals().

  • What if you don't care about the exact object, but only a specific property within it?
  • What if an argument is a new object each time, but its content is what matters?

In these cases, relying solely on exact matches can make your tests fragile.

All lessons in this course

  1. Stubbing Return Values
  2. Mockito Argument Matchers
  3. Spying on Real Objects
  4. Throwing Exceptions and Consecutive Calls
← Back to Testing Mastery: JUnit, Mockito & Integration Tests