Package org.mockito

Class ArgumentCaptor<T>


  • public class ArgumentCaptor<T>
    extends java.lang.Object
    Use it to capture argument values for further assertions.

    Mockito verifies argument values in natural java style: by using an equals() method. This is also the recommended way of matching arguments because it makes tests clean & simple. In some situations though, it is helpful to assert on certain arguments after the actual verification. For example:

    
       ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
       verify(mock).doSomething(argument.capture());
       assertEquals("John", argument.getValue().getName());
     
    Example of capturing varargs:
    
       //capturing varargs:
       ArgumentCaptor<Person> varArgs = ArgumentCaptor.forClass(Person.class);
       verify(mock).varArgMethod(varArgs.capture());
       List expected = asList(new Person("John"), new Person("Jane"));
       assertEquals(expected, varArgs.getAllValues());
     

    Warning: it is recommended to use ArgumentCaptor with verification but not with stubbing. Using ArgumentCaptor with stubbing may decrease test readability because captor is created outside of assert (aka verify or 'then') block. Also it may reduce defect localization because if stubbed method was not called then no argument is captured.

    In a way ArgumentCaptor is related to custom argument matchers (see javadoc for ArgumentMatcher class). Both techniques can be used for making sure certain arguments were passed to mocks. However, ArgumentCaptor may be a better fit if:

    • custom argument matcher is not likely to be reused
    • you just need it to assert on argument values to complete verification
    Custom argument matchers via ArgumentMatcher are usually better for stubbing.

    This utility class *doesn't do any type checks*. The generic signatures are only there to avoid casting in your code.

    There is an annotation that you might find useful: @Captor

    See the full documentation on Mockito in javadoc for Mockito class.

    Since:
    1.8.0
    See Also:
    Captor
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ArgumentCaptor​(java.lang.Class<? extends T> clazz)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T capture()
      Use it to capture the argument.
      static <U,​S extends U>
      ArgumentCaptor<U>
      forClass​(java.lang.Class<S> clazz)
      Build a new ArgumentCaptor.
      java.util.List<T> getAllValues()
      Returns all captured values.
      T getValue()
      Returns the captured value of the argument.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • clazz

        private final java.lang.Class<? extends T> clazz
    • Constructor Detail

      • ArgumentCaptor

        private ArgumentCaptor​(java.lang.Class<? extends T> clazz)
    • Method Detail

      • capture

        public T capture()
        Use it to capture the argument. This method must be used inside of verification.

        Internally, this method registers a special implementation of an ArgumentMatcher. This argument matcher stores the argument value so that you can use it later to perform assertions.

        See examples in javadoc for ArgumentCaptor class.

        Returns:
        null or default values
      • getValue

        public T getValue()
        Returns the captured value of the argument. When capturing varargs use getAllValues().

        If verified method was called multiple times then this method it returns the latest captured value.

        See examples in javadoc for ArgumentCaptor class.

        Returns:
        captured argument value
      • getAllValues

        public java.util.List<T> getAllValues()
        Returns all captured values. Use it when capturing varargs or when the verified method was called multiple times. When varargs method was called multiple times, this method returns merged list of all values from all invocations.

        Example:

        
           mock.doSomething(new Person("John");
           mock.doSomething(new Person("Jane");
        
           ArgumentCaptor<Person> peopleCaptor = ArgumentCaptor.forClass(Person.class);
           verify(mock, times(2)).doSomething(peopleCaptor.capture());
        
           List<Person> capturedPeople = peopleCaptor.getAllValues();
           assertEquals("John", capturedPeople.get(0).getName());
           assertEquals("Jane", capturedPeople.get(1).getName());
         
        Example of capturing varargs:
        
           mock.countPeople(new Person("John"), new Person("Jane"); //vararg method
        
           ArgumentCaptor<Person> peopleCaptor = ArgumentCaptor.forClass(Person.class);
        
           verify(mock).countPeople(peopleCaptor.capture());
        
           List expected = asList(new Person("John"), new Person("Jane"));
           assertEquals(expected, peopleCaptor.getAllValues());
         
        See more examples in javadoc for ArgumentCaptor class.
        Returns:
        captured argument value
      • forClass

        public static <U,​S extends U> ArgumentCaptor<U> forClass​(java.lang.Class<S> clazz)
        Build a new ArgumentCaptor.

        Note that an ArgumentCaptor *doesn't do any type checks*. It is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.

        Type Parameters:
        S - Type of clazz
        U - Type of object captured by the newly built ArgumentCaptor
        Parameters:
        clazz - Type matching the parameter to be captured.
        Returns:
        A new ArgumentCaptor