EasyMock 返回奇怪的值
我目前正在尝试学习如何使用easymock。我有以下代码:
List list = EasyMock.createMock(List.class);
EasyMock.expect(list.size()).andReturn(0);
EasyMock.replay(list);
EasyMock.verify(list);
至少对我来说,这应该可以工作——一个列表被初始化,其中没有任何内容,并且大小应该返回 0。但是,我收到以下错误:
java.lang.AssertionError:
Expectation failure on verify:
size(): expected: 1, actual: 0
我认为这很奇怪,所以我更改了将行中的 0 改为 1 并重新运行测试。我遇到了同样的错误。有谁知道我做错了什么?提前致谢!
I am currently trying to learn how to use easymock. I have the following code:
List list = EasyMock.createMock(List.class);
EasyMock.expect(list.size()).andReturn(0);
EasyMock.replay(list);
EasyMock.verify(list);
This, to me at least, should work -- a list is initialized with nothing in it and the size should return 0. I get the following error, however:
java.lang.AssertionError:
Expectation failure on verify:
size(): expected: 1, actual: 0
I thought this was weird, so I changed the 0 in the line to a 1 and reran the test. I got the same error. Does anyone know what I'm doing wrong? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
replay
之后和verify
之前,您需要调用使用您的模拟的代码。该代码需要调用预期的方法(在本例中为size
),并且仅该方法。错误消息意味着您将模拟设置为期望方法调用,但是当您去验证时,您从未在模拟上调用该方法,这是有道理的,因为您从未使用过模拟。after
replay
and beforeverify
, you need to invoke the code that uses your mock. That code needs to call the expected method (size
in this case) and only that method. The error message means you set your mock up to expect a method call, but when you went to verify, you never called the method on the mock, which makes sense because you never used the mock.