Mockito 抱怨错误的论点

发布于 2024-10-04 03:34:13 字数 542 浏览 0 评论 0原文

我们尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┾廆蒐ゝ 2024-10-11 03:34:13

这有点牵强,但请检查您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文