What's the difference between Mockito Matchers isA, any, eq, and same?

Silly Sally picture Silly Sally · Jun 17, 2015 · Viewed 64.3k times · Source

I am confused on what's the difference between them, and which one to choose in which case. Some difference might be obvious, like any and eq, but I'm including them all just to be sure.

I wonder about their differences because I came across this problem: I have this POST method in a Controller class

public Response doSomething(@ResponseBody Request request) {
    return someService.doSomething(request);
}

And would like to perform a unit test on that controller. I have two versions. The first one is the simple one, like this

@Test
public void testDoSomething() {
    //initialize ObjectMapper mapper
    //initialize Request req and Response res
    
    when(someServiceMock.doSomething(req)).thenReturn(res);

    Response actualRes = someController.doSomething(req);
    assertThat(actualRes, is(res));
}

But I wanted to use a MockMvc approach, like this one

@Test
public void testDoSomething() {
    //initialize ObjectMapper mapper
    //initialize Request req and Response res
    
    when(someServiceMock.doSomething(any(Request.class))).thenReturn(res);

    mockMvc.perform(post("/do/something")
            .contentType(MediaType.APPLICATION_JSON)
            .content(mapper.writeValueAsString(req))
    )
            .andExpect(status().isOk())
            .andExpect(jsonPath("$message", is("done")));
}

Both work well. But I wanted my someServiceMock.doSomething() in the MockMvc approach to receive req, or at least an object that has the same variable values as req (not just any Request class), and return res, just like the first. I know that it's impossible using the MockMvc approach (or is it?), because the object passed in the actual call is always different from the object passed in the mock. Is there anyway I can achieve that? Or does it even make sense to do that? Or should I be satisfied using any(Request.class)? I've tried eq, same, but all of them fail.

Answer

Jeff Bowman picture Jeff Bowman · Jun 17, 2015
  • any() checks absolutely nothing. In Mockito 1.x, any(T.class) also checks absolutely nothing but also saves you a cast (prior to Java 8).

    This is due to change in Mockito 2.0 and beyond, when any(T.class) will share isA semantics to mean "any T" or properly "any instance of type T". any() will still check absolutely nothing.

  • isA(T.class) checks that the argument instanceof T, implying it is non-null.

  • same(obj) checks that the argument is the same instance as obj, such that arg == obj is true.

  • eq(obj) checks that the argument equals obj according to its equals method. This is also the behavior if you pass in real values without using matchers.

    Note that unless equals is overridden, you'll see the default Object.equals implementation, which would have the same behavior as same(obj).

If you need more exact customization, you can use an adapter for your own predicate:

  • For Mockito 1.x, use argThat with a custom Hamcrest Matcher<T> that selects exactly the objects you need.
  • For Mockito 2.0 and beyond, use Matchers.argThat with a custom org.mockito.ArgumentMatcher<T>, or MockitoHamcrest.argThat with a custom Hamcrest Matcher<T>.