使模拟方法返回传递给它的参数
考虑这样的方法签名:
public String myFunction(String abc);
Mockito 可以帮助返回该方法收到的相同字符串吗?
Consider a method signature like:
public String myFunction(String abc);
Can Mockito help return the same string that the method received?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
从 Mockito 1.9.5+ 和 Java 8+ 开始,
您可以使用 lambda 表达式,例如:
对于旧版本,
您可以在 Mockito 中创建答案。假设我们有一个名为 MyInterface 的接口,其方法为 myFunction。
这是带有 Mockito 答案的测试方法:
Since Mockito 1.9.5+ and Java 8+
You can use a lambda expression, like:
For older versions
You can create an Answer in Mockito. Let's assume, we have an interface named MyInterface with a method myFunction.
Here is the test method with a Mockito answer:
如果您有 Mockito 1.9.5 或更高版本,有一个新的静态方法可以为您创建
Answer
对象。您需要编写类似或替代的
内容请注意,
returnsFirstArg()
方法在AdditionalAnswers
类中是静态的,这是 Mockito 1.9.5 中新增的;所以你需要正确的静态导入。If you have Mockito 1.9.5 or higher, there is a new static method that can make the
Answer
object for you. You need to write something likeor alternatively
Note that the
returnsFirstArg()
method is static in theAdditionalAnswers
class, which is new to Mockito 1.9.5; so you'll need the right static import.使用 Java 8,即使使用旧版本的 Mockito,也可以创建一行答案:
当然,这不如使用 David Wallace 建议的
AdditionalAnswers
有用,但如果您想这样做,可能会很有用“即时”转换参数。With Java 8 it is possible to create a one-line answer even with older version of Mockito:
Of course this is not as useful as using
AdditionalAnswers
suggested by David Wallace, but might be useful if you want to transform argument "on the fly".我有一个非常相似的问题。目标是模拟一个持久保存对象并可以按名称返回它们的服务。该服务如下所示:
服务模拟使用映射来存储 Room 实例。
我们现在可以在这个模拟上运行我们的测试。例如:
I had a very similar problem. The goal was to mock a service that persists Objects and can return them by their name. The service looks like this:
The service mock uses a map to store the Room instances.
We can now run our tests on this mock. For example:
使用 Java 8,Steve 的答案 可以变得
编辑:甚至更短:
With Java 8, Steve's answer can become
EDIT: Even shorter:
这是一个很老的问题,但我认为仍然相关。此外,接受的答案仅适用于字符串。与此同时,Mockito 2.1 和一些导入已经改变,所以我想分享我当前的答案:
myClass.myFunction 看起来像:
This is a pretty old question but i think still relevant. Also the accepted answer works only for String. Meanwhile there is Mockito 2.1 and some imports have changed, so i would like to share my current answer:
The myClass.myFunction would look like:
这有点旧了,但我来这里是因为我遇到了同样的问题。我正在使用 JUnit,但这次是在带有 mockk 的 Kotlin 应用程序中。我在这里发布一个示例以供参考并与 Java 对应部分进行比较:
This is a bit old, but I came here because I had the same issue. I'm using JUnit but this time in a Kotlin app with mockk. I'm posting a sample here for reference and comparison with the Java counterpart:
您可以通过使用ArgumentCaptor来实现这一点
想象一下您有像这样的bean函数。
然后在您的测试类中:
或者如果您喜欢 lambda,只需执行以下操作:
摘要:使用 argumentcaptor 来捕获传递的参数。稍后在回答中返回使用 getValue 捕获的值。
You can achieve this by using ArgumentCaptor
Imagine you have bean function like so.
Then in your test class:
OR if you fan of lambda simply do:
Summary: Use argumentcaptor, to capture the parameter passed. Later in answer return the value captured using getValue.
您可能希望将 verify() 与 ArgumentCaptor 结合使用,以确保测试中的执行,并使用 ArgumentCaptor 来评估参数:
参数的值显然可以通过 argument.getValue() 访问,以进行进一步的操作/检查/其他操作。
You might want to use verify() in combination with the ArgumentCaptor to assure execution in the test and the ArgumentCaptor to evaluate the arguments:
The argument's value is obviously accessible via the argument.getValue() for further manipulation / checking /whatever.
我使用类似的东西(基本上是相同的方法)。有时,让模拟对象为某些输入返回预定义的输出很有用。事情是这样的:
I use something similar (basically it's the same approach). Sometimes it's useful to have a mock object return pre-defined output for certain inputs. That goes like this: