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

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

Comments

Projects
None yet
1 participant
@henri-tremblay
Contributor

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 {

@Test
public void testCaptureNonStrictControl(){
    /*
     * Should multiple same-type captures be allowed when order is 
     * not enforced?
     */
    testCaptureHelper(createMock(IProcess.class));
}

@Test
public void testCaptureStrictControl(){     
    testCaptureHelper(createStrictMock(IProcess.class));
}

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

    processMock.process(capture(capture1));
    processMock.process(capture(capture2));

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

    assertTrue(capture1.hasCaptured());
    assertTrue(capture2.hasCaptured());
    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) {
    buffer.append("capture(").append(capture).append(")");
}

@SuppressWarnings("unchecked")
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...

@henri-tremblay henri-tremblay self-assigned this Jun 3, 2015

@henri-tremblay henri-tremblay added this to the 2.5 milestone Jun 3, 2015

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