EasyMock,返回任何对象,还是跳过该函数调用?
使用 EasyMock,如何指定返回“anyObject”?如果我尝试使用“.addReturns(anyObject())
”,则会出现异常。
或者有没有一种方法可以放松 EasyMock 的要求,只是说,如果你调用这个附加方法就可以了?
基本上我的函数正在调用 HttpServletResponse.getWriter() ,我真的不关心它调用它或它返回什么。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的是
http://www.easymock.org/api/easymock/2.4/org/easymock/EasyMock.html#createNiceMock%28java.lang.Class%29
What you are looking for is
http://www.easymock.org/api/easymock/2.4/org/easymock/EasyMock.html#createNiceMock%28java.lang.Class%29
Amir 关于“nice mocks”的回答是使用 EasyMock 执行此操作的正确方法,但也许您最好使用 HttpServletRequest 的完全存根版本,例如 Spring 的
MockHttpServletRequest
,它不是动态模拟,只是提供接口的实现,该接口提供所有方法的直接实现。必须指定要在 servlet 请求/响应上调用的每个方法,听起来似乎很烦人且乏味 - 我认为期望 servlet 层代码与这些请求/响应对象紧密耦合是合理的。
Amir's answer about "nice mocks" is the right way to do this with EasyMock, but perhaps you'd be better off with a fully stubbed-out version of HttpServletRequest such as Spring's
MockHttpServletRequest
, which rather than a dynamic mock just provides an implementation of the interface which provides straight-forward implementations of all the methods.Having to specify each of the methods you want to call on the servlet request/response sounds like it would be tiresome and tedious - I think it's rational to expect that servlet-layer code is pretty tightly coupled to these request/response objects.