Software Development

Capture Argument To Method in Mockito

This post shows how to capture arguments our codes passed to a mock’s method. To do this, a method must accept reference types. Then, we use Mockito.verify() method. Then, pass an instance of ArgumentCaptor to the mock’s method.

Capture Argument In Mockito

Verifying if our codes ran our method is not enough.  We want to see the contents of an argument to the method. To do this, we use the ArgumentCaptor class, as follows.

Consider the following codes. The instance variables service and record are mock instances. The record mocks the getId() and getStatus() methods. Then, we capture arguments to the processLegalAged method using the ArgumentCaptor class.

Download the Codes

The complete codes are available on this link. I added a new test case in com.turreta.mockito.voidmethod.VoidMethodClassTest.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like