Sign in Go Pro


Stubs - Matching on Any Parameters of a Given Type

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson

Up next




This lesson describes how you can configure stubs to match on user-defined objects, as opposed to those classes built into Java.

Mockito has built-in matchers for the likes of anyString(), anyInt(), anyBoolean() and so on, but what happens when you need to match on a class you wrote yourself?

We describe the use of the any() matcher which matches on any types and any values, including nulls.
Additionally, we describe the use of the overloaded any(Foo.class) matcher, which requires an instance of the user-defined class Foo to match; this will not match on nulls.

Summary of Content:

  • How to let a stub respond when you pass null as a parameter
  • Configuring a stub to match on anything (any type and any value) passed as a parameter, which includes nulls
  • Configuring a stub to match on only instances of a specific class, which excludes nulls
  • The differences in syntax required when using Java 7 vs Java 8

Code Links:

Other Relevant Links:




There are no comments on this lesson, start the conversation below ...


Lessons in Mockito