You have to create your own actions. Here is mine:
/**
* puts the parameter array as elements in the list
* @param parameters A mutable list, will be cleared when the Action is invoked.
*/
public static Action captureParameters(final List<Object> parameters) {
return new CustomAction("captures parameters") {
public Object invoke(Invocation invocation) throws Throwable {
parameters.clear();
parameters.addAll(Arrays.asList(invocation.getParametersAsArray()));
return null;
}
};
}
You then use it like this (with a static import):
final List<Object> parameters = new ArrayList<Object>();
final SomeInterface services = context.mock(SomeInterface.class);
context.checking(new Expectations() {{
oneOf(services).createNew(with(6420), with(aNonNull(TransactionAttributes.class)));
will(doAll(captureParameters(parameters), returnValue(true)));
}});
To do what you want, you have to implement your own matcher. This is what I hacked up (some null checking left out, and of course I just use well known interfaces for the sample):
@RunWith(JMock.class)
public class Scrap {
private Mockery context = new JUnit4Mockery();
@Test
public void testCaptureParameters() throws Exception {
final CharSequence mock = context.mock(CharSequence.class);
final ResultSet rs = context.mock(ResultSet.class);
final List<Object> parameters = new ArrayList<Object>();
context.checking(new Expectations(){{
oneOf(mock).charAt(10);
will(doAll(JMockActions.captureParameters(parameters), returnValue((char) 0)));
oneOf(rs).getInt(with(new ParameterMatcher<Integer>(parameters, 0)));
}});
mock.charAt(10);
rs.getInt(10);
}
private static class ParameterMatcher<T> extends BaseMatcher<T> {
private List<?> parameters;
private int index;
private ParameterMatcher(List<?> parameters, int index) {
this.parameters = parameters;
this.index = index;
}
public boolean matches(Object item) {
return item.equals(parameters.get(index));
}
public void describeTo(Description description) {
description.appendValue(parameters.get(index));
}
}
}