【问题标题】:JMock - why does this test fail?JMock - 为什么这个测试失败了?
【发布时间】:2011-08-04 20:44:43
【问题描述】:

我在玩 JMock 并在基本测试中遇到此错误:有人知道为什么吗?

unexpected invocation: class2.add(<4>, <4>)
expectations:
 expected once, never invoked: class2.add(<2>, <2>); returns <4>
what happened before this: nothing!

这是课

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;

public class Play {
    private Mockery context = new Mockery();

    private Class2 mockedClass = context.mock(Class2.class);

    @Test
    public void testMethod() {

        Class1 class1 = new Class1();
        class1.class2 = mockedClass;

        context.checking(new Expectations() {
            {
                oneOf(mockedClass).add(2, 2);
                will(returnValue(4));
            }
        });

        class1.add(4, 4);
        context.assertIsSatisfied();
    }

    public class Class1 {
        public Class2 class2;

        public Integer add(Integer value1, Integer value2) {
        Integer val = class2.add(value1, value2);

        return val;
    }
}

public interface Class2 {
    public Integer add(Integer value1, Integer value2);
}

public class Class2Impl implements Class2 {
    public Integer add(Integer value1, Integer value2) {
        return value1 + value2;
    }
}

}

谢谢

【问题讨论】:

    标签: unit-testing jmock


    【解决方案1】:

    我设法让这个工作。我只需要更改这一行

    oneOf(mockedClass).add(2, 2);

    到

    oneOf(mockedClass).add(4, 4);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      相关资源
      最近更新 更多