如何在 easymock 中模拟应返回其参数之一的方法?
public Object doSomething(Object o);
我想模拟它。它应该只返回它的参数。我尝试过:
Capture<Object> copyCaptcher = new Capture<Object>();
expect(mock.doSomething(capture(copyCaptcher)))
.andReturn(copyCatcher.getValue());
但没有成功,我只得到一个 AssertionError 作为 java.lang.AssertionError: Nothing capture Yet
。有什么想法吗?
public Object doSomething(Object o);
which I want to mock. It should just return its parameter. I tried:
Capture<Object> copyCaptcher = new Capture<Object>();
expect(mock.doSomething(capture(copyCaptcher)))
.andReturn(copyCatcher.getValue());
but without success, I get just an AssertionError as java.lang.AssertionError: Nothing captured yet
. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,最简单的方法是在 IAnswer 实现中使用 Capture...当然,在内联执行此操作时,您必须将其声明为
final
。这可能是最准确的方法,无需依赖某些上下文信息。这每次都对我有用。
Well, the easiest way would be to just use the Capture in the IAnswer implementation... when doing this inline you have to declare it
final
of course.This is probably the most exact way, without relying on some context information. This does the trick for me every time.
我一直在寻找相同的行为,最后写了以下内容:
我在类的 javadoc 中写了一个快速的“如何做”。
希望这有帮助。
I was looking for the same behavior, and finally wrote the following :
I wrote a quick "how to" in the javadoc of the class.
Hope this helps.
捕获用于测试随后传递给模拟的值。如果你只需要一个模拟来返回一个参数(或从参数计算出的一些值),你只需要实现 IAnswer。
如果您想要一种可重用的方式将参数 X 传回,请参阅“Remi Fouilloux”的实现,但请忽略他在示例中对 Capture 的使用。
如果您只想像“does_the_trick”的示例那样内联它,那么 Capture 在这里是一个转移注意力的事情。这是简化版本:
Captures are for testing the values passed to the mock afterwards. If you only need a mock to return a parameter (or some value calculated from the parameter), you just need to implement IAnswer.
See "Remi Fouilloux"s implementation if you want a reusable way of passing paramter X back, but ignore his use of Capture in the example.
If you just want to inline it like "does_the_trick"s example, again, the Capture is a red herring here. Here is the simplified version:
基于@does_the_trick并使用lambdas,您现在可以编写以下内容:
或者按照@thetoolman的建议不进行捕获
Based on @does_the_trick and using lambdas, you can now write the following:
or without capture as @thetoolman suggested
嗯,如果我正确理解你的问题,我认为你可能过于复杂化了。
<代码>
对象 someObject = .... ;
Expect(mock.doSomething(someObject)).andReturn(someObject);
应该
可以正常工作。请记住,您正在提供预期的参数和返回值。因此在两个作品中使用相同的对象。
Um, if I understand your question correctly I think you may be over complicating it.
Object someObject = .... ;
expect(mock.doSomething(someObject)).andReturn(someObject);
Should work just fine. Remember you are supplying both the expected parameter and returne value. So using the same object in both works.