EasyMock Capture Arguments With Examples

Sometimes we want to stub behaviors for any input arguments, so we use argument matchers. If you want to know the arguments when these stub methods are being called, we have to use matcher that capture argument.

EasyMock Capture Arguments

EasyMock capture arguments require following steps:

  • Create Capture instance using EasyMock.newCapture() method.
  • Use capture(Capture) argument matcher with expect to match any argument and also capture it for later use.
  • If you want to capture primitive types, there are specific methods such as captureInt(), captureBoolean() etc.

Single Argument Capture

If we capture a single argument, then we can get its value from Capture.getValue() method. Let’s have a quick example to capture single argument.

Capture Arguments for Multiple Calls

If the stubbed method is called multiple times, we can use getValues() to get the list of arguments.

Capture Multiple Arguments

We can also capture multiple arguments methods. We can use different Capture instances for the different argument types or use the same instance.

Notice that I am using captureInt() in above example to capture integer arguments.


EasyMock argument capture helps us in tracking and recording the arguments used with mocked methods. This can be helpful in debugging if our test cases are failing for any specific argument.

You can checkout complete project and more EasyMock examples from our GitHub Repository.

By admin

Leave a Reply

%d bloggers like this: