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?
EasyMock provides various methods to create mock objects. EasyMock. createMock() creates mocks without bothering about the order of method calls that the mock is going to make in due course of its action.
If you still want to use EasyMock, because changing it doesn't depend on you (work in an enterprise) you can use reflection to change the private field which your method returns, or any private field for that matter. You can have these methods in a helper class for example. And use them as needed.
Well, the easiest way would be to just use the Capture in the IAnswer implementation... when doing this inline you have to declare it final
of course.
MyService mock = createMock(MyService.class); final Capture<ParamAndReturnType> myCapture = new Capture<ParamAndReturnType>(); expect(mock.someMethod(capture(myCapture))).andAnswer( new IAnswer<ParamAndReturnType>() { @Override public ParamAndReturnType answer() throws Throwable { return myCapture.getValue(); } } ); replay(mock)
This is probably the most exact way, without relying on some context information. This does the trick for me every time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With