Monday, March 12, 2012

Mockito Crib Sheet #2

[Continued...]

5. Capturing arguments passed to a mock:
       String expected = "test string";

toTest.hitTakeArgumentReturnVoid(expected);

ArgumentCaptor stringCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(mock).takeArgumentReturnVoid(stringCaptor.capture());
Assert.assertEquals(expected, stringCaptor.getValue());
Note that this capture code can come after the call to the production method.

6. Verifying that a method is never called:
       Mockito.verify(mock, Mockito.never()).takeArguments(Mockito.anyString(), Mockito.anyInt());

Or, equivalently:
      
Mockito.verify(mock, Mockito.times(0)).takeArguments(Mockito.anyString(), Mockito.anyInt());

But I don't like this as the set of methods never called is infinite.

No comments:

Post a Comment