JMock - 为什么这个测试失败?
我正在使用 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;
}
}
}
谢谢
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) {
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法让这个工作。我只需更改这一行
oneOf(mockedClass).add(2, 2);
到
oneOf(mockedClass).add(4, 4);
I managed to get this working. I just had to change this line
oneOf(mockedClass).add(2, 2);
to
oneOf(mockedClass).add(4, 4);