[英]what is the correct way to make mocks when testing a method?
我是使用模拟的新手,特别是jMock (对于Java)。 我之前已经做过一些测试,但是没有使用模拟,我想知道制作它们的正确方法是什么。
例如,我有一个带有实现的服务..因此,我有这个服务测试。 在服务器测试中,我有自己的方法。
可以说我有methodA
上serviceHelperTest
,并作为的一部分methodA
我有这
ArrayList someValues = serviceHelperImpl.otherMethod
因此,据我所知,我应该模拟此serviceHelperImpl.othermethod
,因为我并不真正在乎是否好,它应该返回一些值。
因此,假设我期望一个boolean
值,可以这样做吗?
public myMethodToTest(){
mockery.checking(new Expectations() {
{
oneOf(serviceHelper).otherMethod();
will(returnValue(true));
}
});
Boolean myVar = serviceHelper.otherMethod();
...
}
我不知道这是否正确...知道如何在测试单元上实现模拟吗?
我不太确定为什么要尝试从serviceHelper.otherMethod();
保存返回值serviceHelper.otherMethod();
如果这样做的目的是测试otherMethod
,则应仅调用otherMethod
serviceHelper.otherMethod();
otherMethod
而不将返回值保存在变量中。 请记住,模拟测试是检查功能的“状态”,而不是结果。 如果要检查结果,则应使用xUnit测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.