Mockito. Verify method arguments

An alternative to ArgumentMatcher is ArgumentCaptor. Official example: ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class); verify(mock).doSomething(argument.capture()); assertEquals(“John”, argument.getValue().getName()); A captor can also be defined using the @Captor annotation: @Captor ArgumentCaptor<Person> captor; //… MockitoAnnotations.initMocks(this); @Test public void test() { //… verify(mock).doSomething(captor.capture()); assertEquals(“John”, captor.getValue().getName()); }

How to capture a list of specific type with mockito

The nested generics-problem can be avoided with the @Captor annotation: public class Test{ @Mock private Service service; @Captor private ArgumentCaptor<ArrayList<SomeType>> captor; @Before public void init(){ MockitoAnnotations.initMocks(this); } @Test public void shouldDoStuffWithListValues() { //… verify(service).doStuff(captor.capture())); } }

Different return values the first and second time with Moq

With the latest version of Moq(4.2.1312.1622), you can setup a sequence of events using SetupSequence. Here’s an example: _mockClient.SetupSequence(m => m.Connect(It.IsAny<String>(), It.IsAny<int>(), It.IsAny<int>())) .Throws(new SocketException()) .Throws(new SocketException()) .Returns(true) .Throws(new SocketException()) .Returns(true); Calling connect will only be successful on the third and fifth attempt otherwise an exception will be thrown. So for your example it would … Read more