|
|
1
19
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];
}
}
我在班上的javadoc里写了一个简短的“如何做”。 希望这有帮助。 |
|
|
2
28
嗯,最简单的方法就是在IAnswer实现中使用捕获。。。在内联执行此操作时,必须声明它
这可能是最准确的方法,而不依赖于某些上下文信息。每次都是这样。 |
|
|
3
17
如果您想要一种可重用的方式将参数X传递回来,请参阅“Remi Fouilloux”的实现,但是忽略他在示例中对捕获的使用。 如果您只想像“does the the theu trick”的示例那样内联它,那么这里的捕获是一条红鲱鱼。以下是简化版本:
|
|
|
4
9
基于@does theu技巧并使用lambdas,您现在可以编写以下内容:
或者像“工具人”建议的那样不抓捕
|
|
|
5
2
嗯,如果我没弄错你的问题,我想你可能把问题复杂化了。
应该很好用。请记住,您提供的是预期参数和returne值。所以在两个作品中使用相同的对象。 |