Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capturing parameters from single argument methods #24

henri-tremblay opened this issue Jun 3, 2015 · 0 comments

Capturing parameters from single argument methods #24

henri-tremblay opened this issue Jun 3, 2015 · 0 comments


Copy link

henri-tremblay commented Jun 3, 2015

Migrated from: CodeHaus issue EASYMOCK-13
Original reporter: Henri Tremblay

First off, I'm very happy with EasyMock, it has saved me many hours of work.

Not sure if this is a bug or intended behaviour.

Having read bug 2060545
However, EasyMock behavior is perfectly normal. Captures doen't occur in
order. They occur when a call is matched.

What should happen in the case of a mock method with a single parameter that is called twice?

import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.easymock.Capture;
import org.junit.Test;

public class MultipleCaptureTest {

public void testCaptureNonStrictControl(){
     * Should multiple same-type captures be allowed when order is 
     * not enforced?

public void testCaptureStrictControl(){     

protected void testCaptureHelper(IProcess processMock){
    Capture<?> capture1 = new Capture();
    Capture<?> capture2 = new Capture();


    processMock.process(new Object());
    processMock.process(new Object());

    assertFalse(capture1.getValue() == capture2.getValue());

public interface IProcess {
    public void process(Object object);


Perhaps something like this would work....

public class Captures implements IArgumentMatcher, Serializable {

private static final long serialVersionUID = 1L;

private Capture<T> capture;

public Captures(Capture<T> captured) {
    this.capture = captured;

public void appendTo(StringBuffer buffer) {

public boolean matches(Object actual) {
     * If the capture has already been done then this capture
     * cannot be used
    if (!capture.hasCaptured()){
        capture.setValue((T) actual);
        return true;
    } else {
        return false;


Although this breaks one of the unit tests...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

1 participant