public Object doSomething(Object o); which I want to mock. It should just return its parameter. I tried:
Capture<Object> copyCaptcher = new Capture<Object>();
expect(mock.doSomething(capture(copyCaptcher)))
.andReturn(copyCatcher.getValue());
but without success, I get just an AssertionError as java.lang.AssertionError: Nothing captured yet. Any ideas?
I was looking for the same behavior, and finally wrote the following :
import org.easymock.EasyMock; import org.easymock.IAnswer; /** * Enable a Captured argument to be answered to an Expectation. * For example, the Factory interface defines the following * <pre> * CharSequence encode(final CharSequence data); * </pre> * For test purpose, we don't need to implement this method, thus it should be mocked. * <pre> * final Factory factory = mocks.createMock("factory", Factory.class); * final ArgumentAnswer<CharSequence> parrot = new ArgumentAnswer<CharSequence>(); * EasyMock.expect(factory.encode(EasyMock.capture(new Capture<CharSequence>()))).andAnswer(parrot).anyTimes(); * </pre> * Created on 22 juin 2010. * @author Remi Fouilloux * */ public class ArgumentAnswer<T> implements IAnswer<T> { private final int argumentOffset; public ArgumentAnswer() { this(0); } public ArgumentAnswer(int offset) { this.argumentOffset = offset; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public T answer() throws Throwable { final Object[] args = EasyMock.getCurrentArguments(); if (args.length < (argumentOffset + 1)) { throw new IllegalArgumentException("There is no argument at offset " + argumentOffset); } return (T) args[argumentOffset]; } }I wrote a quick “how to” in the javadoc of the class.
Hope this helps.