开发者

JMock - why does this test fail?

I'm playing around with JMock and get this error on a basic test: Does anyone know why?

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

This is the class

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) {
     开发者_StackOverflow中文版   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;
    }
}

}

Thanks


I managed to get this working. I just had to change this line

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

to

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜