Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 265
Silly Sally Avatar asked Jun 17 '15 11:06

Silly Sally


People also ask

What does EQ mean in Mockito?

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

What are Mockito matchers?

What are Matchers? Matchers are like regex or wildcards where instead of a specific input (and or output), you specify a range/type of input/output based on which stubs/spies can be rest and calls to stubs can be verified. All the Mockito matchers are a part of 'Mockito' static class.

What can I use instead of Mockito matchers?

mockito. Matchers is deprecated, ArgumentMatchers should be used instead.

What is the use of Mockito Any ()?

Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when() and thenReturn() on the mock object.

Why do we use Argument matchers instead of equal () method in Mockito?

It extends ArgumentMatchers class to access all the matcher functions. Mockito uses equal () as a legacy method for verification and matching of argument values. In some cases, we need more flexibility during the verification of argument values, so we should use argument matchers instead of equal () method.

What are the different types of matchers in Mockito?

There are broadly 2 types of matchers in Mockito or in terms of usage, matchers can be used for the below 2 categories: For both types of Matchers i.e. Argument and Verification, Mockito provides a huge set of matchers (Click here to get a complete list of the matchers).

What is the use of any () and EQ in mocks in Mockito?

Mockito provides any () (and similar methods) and eq to match method parameters when setting up mocks and verifying that they are called. 8 clever moves when you have $1,000 in the bank. We've put together a list of 8 money apps to get you on the path towards a bright financial future.

How do I check if two objects are equal in Mockito?

For Mockito 2.0 and beyond, use Matchers.argThatwith a custom org.mockito.ArgumentMatcher<T>, or MockitoHamcrest.argThatwith a custom Hamcrest Matcher<T>. You may also use refEq, which uses reflectionto confirm object equality; Hamcrest has a similar implementation with SamePropertyValuesAsfor public bean-style properties.


2 Answers

  • any() checks absolutely nothing. Since Mockito 2.0, any(T.class) shares isA semantics to mean "any T" or properly "any instance of type T".

    This is a change compared to Mockito 1.x, where any(T.class) checked absolutely nothing but saved a cast prior to Java 8: "Any kind object, not necessary of the given class. The class argument is provided only to avoid casting."

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

  • same(obj) checks that the argument refers to 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>.

You may also use refEq, which uses reflection to confirm object equality; Hamcrest has a similar implementation with SamePropertyValuesAs for public bean-style properties. Note that on GitHub issue #1800 proposes deprecating and removing refEq, and as in that issue you might prefer eq to better give your classes better encapsulation over their sense of equality.

like image 164
Jeff Bowman Avatar answered Sep 18 '22 13:09

Jeff Bowman


If your Request.class implements equals, then you can use eq():

Bar bar = getBar(); when(fooService.fooFxn(eq(bar)).then... 

The above when would activate on

fooService.fooFxn(otherBar); 

if

otherBar.equals(bar); 

Alternatively, if you want to the mock to work for some other subset of input (for instance, all Bars with Bar.getBarLength()>10), you could create a Matcher. I don't see this pattern too often, so usually I create the Matcher as a private class:

private static class BarMatcher extends BaseMatcher<Bar>{ ...//constructors, descriptions, etc.   public boolean matches(Object otherBar){      //Checks, casts, etc.      return otherBar.getBarLength()>10;   } } 

You would then use this matcher as follows:

when(fooService.fooFxn(argThat(new BarMatcher())).then... 

Hope that helps!

like image 38
Alex Pruss Avatar answered Sep 20 '22 13:09

Alex Pruss