EasyMock Capture Arguments

Filed Under: EasyMock

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.

package com.journaldev.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;

import org.easymock.Capture;
import org.easymock.CaptureType;
import org.junit.jupiter.api.Test;

import com.journaldev.utils.MathUtils;

public class EasyMockCaptureAgrumentExample {

	public void test_single_call_arg_capture() {
		ArrayList<String> mockList = mock(ArrayList.class);
		Capture captureSingleArgument = newCapture();

		assertTrue(mockList.add("Hello Pankaj"));
		System.out.println(captureSingleArgument.getValue()); //Hello Pankaj

Capture Arguments for Multiple Calls

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

public void test_multiple_calls_args_catcher() {
	ArrayList mockList = mock(ArrayList.class);
	Capture captureArguments = newCapture(CaptureType.ALL);

	System.out.println(captureArguments.getValues()); //[10, 20]

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.

public void test_multiple_args_catcher() {
	MathUtils mock = mock(MathUtils.class);
	Capture<Integer> captureArguments = newCapture(CaptureType.ALL);

	expect(mock.add(captureInt(captureArguments), captureInt(captureArguments))).andReturn(10).times(2);

	assertEquals(mock.add(0,10), 10);
	assertEquals(mock.add(1, 9), 10);
	System.out.println(captureArguments.getValues()); //[0, 10, 1, 9]


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.


  1. Luis Colorado says:

    As you use `Capture`, you could also use `ArrayList` in your sample code, more in behalf of being coherent, or not use generics at all (If you want to reach pre 5.0 java support) but using a mixed style is not appropiate for who is trying to teach something.

    My apologies for the sermon, 馃檪

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors