Mockito 抱怨错误的论点
我们尝试使用 Mockito 验证操作的行为。测试代码如下所示
final Type1 mock = mock(Type1.class);
new SomeAction<Type1>(mock).actionPerformed(null);
verify(mock).someMethod();
方法 actionPerformed 仅包含对 Type1 构造函数中提供的对象的 someMethod 调用。然而 Mockito 抱怨预期的方法调用没有发生,而是发生了不同的方法调用。但是 Mockito 打印的两个调用的 String 表示形式是完全相同的!
有什么解释吗?
更新:来自 Mockito 的错误消息
Argument(s) are different! Wanted:
type1.someMethod();
-> at xxx
Actual invocation has different arguments:
type1.someMethod();
-> at xxx
We try to verify the behaviour of an action with Mockito. The test code looks like this
final Type1 mock = mock(Type1.class);
new SomeAction<Type1>(mock).actionPerformed(null);
verify(mock).someMethod();
The method actionPerformed contains just the call of someMethod on the object provided in the constructor of Type1. Yet Mockito complains that the expected method call did not happen, instead a different method call happened. But the String representation of the two calls printed by Mockito are exactly the same!
Any explanation what is going on?
Update: ErrorMessage from Mockito
Argument(s) are different! Wanted:
type1.someMethod();
-> at xxx
Actual invocation has different arguments:
type1.someMethod();
-> at xxx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点牵强,但请检查您的 toString 实现。我遇到过一些令人恼火的单元测试场景,从单元测试的角度来看,预期的和观察到的似乎是相同的,而实际上它们是不同的。最终,toString 的一个变体让我相信存在相似性,但实际上并不存在。
This is a bit of a stretch, but check your toString implementations. I've ran into some irritating unit test scenarios where the expected and observed appeared to be the same from the unit test point of view when in reality they were different. In the end it was a variation in toString that caused me to believe there was a similarity when in reality there was not.