java - Verify object attribute value with mockito

ID : 10269

viewed : 77

Tags : javamockingmockitojava

Top 5 Answer for java - Verify object attribute value with mockito

vote vote


New feature added to Mockito makes this even easier,

ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class); verify(mock).doSomething(argument.capture()); assertEquals("John", argument.getValue().getName()); 

Take a look at Mockito documentation

In case when there are more than one parameters, and capturing of only single param is desired, use other ArgumentMatchers to wrap the rest of the arguments:

verify(mock).doSomething(eq(someValue), eq(someOtherValue), argument.capture()); assertEquals("John", argument.getValue().getName()); 
vote vote


I think the easiest way for verifying an argument object is to use the refEq method:


It can be used even if the object doesn't implement equals(), because reflection is used. If you don't want to compare some fields, just add their names as arguments for refEq.

vote vote


One more possibility, if you don't want to use ArgumentCaptor (for example, because you're also using stubbing), is to use Hamcrest Matchers in combination with Mockito.

import org.mockito.Mockito import org.hamcrest.Matchers ...  Mockito.verify(mockedObject).someMethodOnMockedObject(MockitoHamcrest.argThat(     Matchers.<SomeObjectAsArgument>hasProperty("propertyName", desiredValue))); 
vote vote


This is answer based on answer from iraSenthil but with annotation (Captor). In my opinion it has some advantages:

  • it's shorter
  • it's easier to read
  • it can handle generics without warnings


@RunWith(MockitoJUnitRunner.class) public class SomeTest{      @Captor     private ArgumentCaptor<List<SomeType>> captor;      //...      @Test      public void shouldTestArgsVals() {         //...         verify(mockedObject).someMethodOnMockedObject(captor.capture());          assertThat(captor.getValue().getXXX(), is("expected"));     } } 
vote vote


If you're using Java 8, you can use Lambda expressions to match.

import java.util.Optional; import java.util.function.Predicate;  import org.hamcrest.BaseMatcher; import org.hamcrest.Description;  public class LambdaMatcher<T> extends BaseMatcher<T> {     private final Predicate<T> matcher;     private final Optional<String> description;      public LambdaMatcher(Predicate<T> matcher)     {         this(matcher, null);     }      public LambdaMatcher(Predicate<T> matcher, String description)     {         this.matcher = matcher;         this.description = Optional.ofNullable(description);     }      @SuppressWarnings("unchecked")     @Override     public boolean matches(Object argument)     {         return matcher.test((T) argument);     }      @Override     public void describeTo(Description description)     {         this.description.ifPresent(description::appendText);     } } 

Example call

@Test public void canFindEmployee() {     Employee employee = new Employee("John");     company.addEmployee(employee);      verify(mockedDal).registerEmployee(argThat(new LambdaMatcher<>(e -> e.getName()                                                                          .equals(employee.getName())))); } 

More info:

Top 3 video Explaining java - Verify object attribute value with mockito