从EasyMock 1转换为EasyMock 2/3时不再调用模拟 [英] Mock No Longer Being Called When Converting from EasyMock 1 to EasyMock 2/3

查看:111
本文介绍了从EasyMock 1转换为EasyMock 2/3时不再调用模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下EasyMock 1代码的测试:

I have a test that includes the following EasyMock 1 code:

persistenceManager.getCount(linkCodeAttributeCriteria);
persistenceManagerControl.setDefaultReturnValue(0);
persistenceManagerControl.replay();
//Run a method
persistenceManagerControl.verify();

现在我的公司最终正在升级其EasyMock代码,我已经更改了将其添加到以下代码中:

Now that my company is finally upgrading their EasyMock code, I have changed it to the following code:

expect(persistenceManager.getCount(linkCodeAttributeCriteria)).andReturn(0);
replay(persistenceManager);
//Run a method
verify(persistenceManager);

但是突然之间,测试失败,说getCount应该被调用一次,但是被调用了0次。这是我接触过的唯一一段代码。为什么此测试失败?

But suddenly the test fails saying that getCount was expected to be called one time, but was called 0 times. This is the only piece of code I have touched. Why is this test failing?

推荐答案

在EasyMock 1中,MockControl有两种返回方法: setReturnValue () setDefaultReturnValue()。尽管相似,但是它们有一个细微的区别:第一个期望该方法被调用一次,第二个期望该方法被调用零次或更多次。问题中的代码使用后者。

In EasyMock 1, there were two return methods for MockControl: setReturnValue() and setDefaultReturnValue(). Although similar, they have a subtle distinction: the first expects the method to be called one time, the second expects the method to be called zero or more times. The code in the question uses the latter.

换句话说:

EasyMock 1                                  | EasyMock 2 and EasyMock 3
---------------------------------------------------------------------------------
setDefaultReturnValue(o)                    | andReturn(o).anyTimes()
setReturnValue(o, MockControl.ZERO_OR_MORE) | andReturn(o).anyTimes()
setReturnValue(o)                           | andReturn(o) or andReturn(o).once()
setReturnValue(o, 1)                        | andReturn(o) or andReturn(o).once()

实际上,您会注意到在EasyMock 1中, setDefaultReturnValue(o)等效于 .setReturnValue(o,MockControl.ZERO_OR_MORE)。将等效性替换为您的旧代码仍然可以使其运行,但是删除参数的次数或将其更改为其他任何次数都将导致测试失败,因为该方法的调用次数不足。

In fact, you'll notice that in EasyMock 1, setDefaultReturnValue(o) is equivalent to .setReturnValue(o, MockControl.ZERO_OR_MORE). Substituting the equivalency into your old code will still make it run, but dropping the number of times argument or changing it to anything else will cause the test to fail because the method was not called enough times.

EasyMock的开发者似乎决定简化操作,只进行一次回叫(考虑到这种混乱,这可能是一个好举动), andReturn(o),而不是两个不同的变量,以及通过 .anyTimes()进行零次或多次显式调用。与EasyMock 1 setReturnValue()一样,一次仍然是EasyMock 2和3中的默认值,可以使用隐式 andReturn(o )或带有显式 andReturn(o).once()

It seems that the developers of EasyMock decided to simplify things by only having one return call (probably a good move, given this confusion), andReturn(o), rather than two different ones, as well as making a "zero times or more" and explicit call via .anyTimes(). As with EasyMock 1 setReturnValue(), one time is still the default in EasyMock 2 and 3, which can either be called with an implied andReturn(o) or with an explicit andReturn(o).once().

如果您需要将行为保持为EasyMock 2/3格式,然后将 setDefaultReturnValue(o)替换为 andReturn(o).anyTimes()

If you need to keep the behavior in EasyMock 2/3 format, then replace setDefaultReturnValue(o) with andReturn(o).anyTimes().

这篇关于从EasyMock 1转换为EasyMock 2/3时不再调用模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆