代码之家  ›  专栏  ›  技术社区  ›  Jan

如何在easymock中模拟一个将返回其参数之一的方法?

  •  30
  • Jan  · 技术社区  · 16 年前

    public Object doSomething(Object o);

    Capture<Object> copyCaptcher = new Capture<Object>();
    expect(mock.doSomething(capture(copyCaptcher)))
            .andReturn(copyCatcher.getValue());
    

    但没有成功,我只得到一个断言错误 java.lang.AssertionError: Nothing captured yet . 有什么想法吗?

    5 回复  |  直到 12 年前
        1
  •  19
  •   thetoolman    14 年前

    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
  •   does_the_trick    15 年前

    嗯,最简单的方法就是在IAnswer实现中使用捕获。。。在内联执行此操作时,必须声明它 final

    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)
    

    这可能是最准确的方法,而不依赖于某些上下文信息。每次都是这样。

        3
  •  17
  •   thetoolman    14 年前

    如果您想要一种可重用的方式将参数X传递回来,请参阅“Remi Fouilloux”的实现,但是忽略他在示例中对捕获的使用。

    如果您只想像“does the the theu trick”的示例那样内联它,那么这里的捕获是一条红鲱鱼。以下是简化版本:

    MyService mock = createMock(MyService.class);
    expect(mock.someMethod(anyObject(), anyObject()).andAnswer(
        new IAnswer<ReturnType>() {
            @Override
            public ReturnType answer() throws Throwable {
                // you could do work here to return something different if you needed.
                return (ReturnType) EasyMock.getCurrentArguments()[0]; 
            }
        }
    );
    replay(mock)
    
        4
  •  9
  •   Marco Baumeler    11 年前

    基于@does theu技巧并使用lambdas,您现在可以编写以下内容:

    MyService mock = EasyMock.createMock(MyService.class);
    
    final Capture<ParamAndReturnType> myCapture = EasyMock.newCapture();
    expect(mock.someMethod(capture(myCapture))).andAnswer(() -> myCapture.getValue());
    

    或者像“工具人”建议的那样不抓捕

    expect(mock.someMethod(capture(myCapture)))
    .andAnswer(() -> (ParamAndReturnType)EasyMock.getCurrentArguments()[0]);
    
        5
  •  2
  •   drekka    16 年前

    嗯,如果我没弄错你的问题,我想你可能把问题复杂化了。

    Object someObject = .... ;
    expect(mock.doSomething(someObject)).andReturn(someObject);

    应该很好用。请记住,您提供的是预期参数和returne值。所以在两个作品中使用相同的对象。

    推荐文章