1. 验证参数

package mockito;

import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class ArgumentCaptorTest {

    /**
     * argument.capture() 捕获方法参数
     * argument.getValue() 获取方法参数值,如果方法进行了多次调用,它将返回最后一个参数值
     * argument.getAllValues() 方法进行多次调用后,返回多个参数值
     */
    @Test
    public void argumentCaptorTest() {
        List mock1 = mock(List.class);
        List mock2 = mock(List.class);

        mock1.add("John");

        mock2.add("Brian");
        mock2.add("Jim");

        ArgumentCaptor argument = ArgumentCaptor.forClass(String.class);

        verify(mock1).add(argument.capture());
        assertEquals("John", argument.getValue());

        verify(mock2, times(2)).add(argument.capture());
        assertEquals("Jim", argument.getValue());
        assertArrayEquals(new Object[]{"John", "Brian", "Jim"}, argument.getAllValues().toArray());
    }

}