如何在 EasyMock 中验证方法调用并忽略返回值?

发布于 2024-07-13 19:06:45 字数 777 浏览 6 评论 0 原文

我在尝试做一件简单的事情时感到沮丧 - 我想在模拟对象上调用方法而不检查其返回值。 我只是想检查它是否使用正确的参数调用。

示例:

MyInterface mockObject = createMock(MyInterface.class);
SomeObject param = new SomeObject();

/* the write object is not void and returns an instance of FooOjbect.
 * I want to ignore everything to do with FooObject - I do not care what
 * it is because I do not store its value. How do I do this? */
mockObject.write(param);

replay(mockObject);

someOtherObjectThatCallsAboveMockObject.process(mockObject);

verify(mockObject);

那么有 EasyMock 专家吗? 我不关心我调用的底层方法的设计,也不存储返回值,因为实际实现来自第三方网络库(Apache Mina),并且我无法控制 API。

编辑: 在一段时间后得出结论

我放弃了 EasyMock,因为它并不容易,转而使用 Mockito

I'm getting frustrated trying to do a simple thing - I want to invoke a method on a mock object and NOT check its return value. I just want to check that it was invoked with the correct parameters.

Example:

MyInterface mockObject = createMock(MyInterface.class);
SomeObject param = new SomeObject();

/* the write object is not void and returns an instance of FooOjbect.
 * I want to ignore everything to do with FooObject - I do not care what
 * it is because I do not store its value. How do I do this? */
mockObject.write(param);

replay(mockObject);

someOtherObjectThatCallsAboveMockObject.process(mockObject);

verify(mockObject);

So are there any EasyMock experts out there? I'm not concerned about the design of the underlying method that I'm calling and not storing the return value because the actually implementation is coming from a third-party networking library (Apache Mina) and I have no control over the API.

EDIT:
Conclusion reached some time later

I dumped EasyMock because it wasn't easy and went for Mockito.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2024-07-20 19:06:45

而不是

mockObject.write(param)

写入。

EasyMock.expect( mockObject.write(param) ).andReturn( /* return value here */ );

您仍然需要返回一个值以使代码正确,但您可以在测试中进一步忽略该值,

Instead of

mockObject.write(param)

write

EasyMock.expect( mockObject.write(param) ).andReturn( /* return value here */ );

You still need to return a value for the code to be correct but you can ignore the value further on in the test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文