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.
Table of Contents
EasyMock Capture Arguments
EasyMock capture arguments require following steps:
- Create
Capture
instance usingEasyMock.newCapture()
method. - Use
capture(Capture)
argument matcher withexpect
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 {
@Test
public void test_single_call_arg_capture() {
ArrayList<String> mockList = mock(ArrayList.class);
Capture captureSingleArgument = newCapture();
expect(mockList.add(capture(captureSingleArgument))).andReturn(true);
replay(mockList);
assertTrue(mockList.add("Hello Pankaj"));
System.out.println(captureSingleArgument.getValue()); //Hello Pankaj
verify(mockList);
}
}
Capture Arguments for Multiple Calls
If the stubbed method is called multiple times, we can use getValues()
to get the list of arguments.
@Test
public void test_multiple_calls_args_catcher() {
ArrayList mockList = mock(ArrayList.class);
Capture captureArguments = newCapture(CaptureType.ALL);
expect(mockList.add(captureInt(captureArguments))).andReturn(true).times(2);
replay(mockList);
assertTrue(mockList.add(10));
assertTrue(mockList.add(20));
System.out.println(captureArguments.getValues()); //[10, 20]
verify(mockList);
}
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.
@Test
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);
replay(mock);
assertEquals(mock.add(0,10), 10);
assertEquals(mock.add(1, 9), 10);
System.out.println(captureArguments.getValues()); //[0, 10, 1, 9]
verify(mock);
}
Notice that I am using captureInt()
in above example to capture integer arguments.
Summary
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.
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, 🙂