简体   繁体   English

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

[英]How do I verify a method call and ignore the return value in EasyMock?

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.尝试做一件简单的事情时我感到很沮丧——我想在模拟 object 上调用一个方法,而不是检查它的返回值。 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?那么有没有 EasyMock 专家呢? 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.我不关心我调用的底层方法的设计而不是存储返回值,因为实际实现来自第三方网络库(Apache Mina),我无法控制 API。

EDIT: Conclusion reached some time later编辑:一段时间后得出结论

I dumped EasyMock because it wasn't easy and went for Mockito .我抛弃了 EasyMock,因为它并不容易,而是选择了Mockito

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.您仍然需要返回一个值才能使代码正确,但您可以在测试中进一步忽略该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM