Mockito 的问题 - excepts.verification.WantedButNotInvoked

发布于 2024-09-06 18:15:14 字数 265 浏览 4 评论 0原文

当我之前在测试中放置“VerificationModeFactory.times(2)”时,当我运行该类的所有测试时,会出现此异常:

org.mockito.exceptions.verification.WantedButNotInvoked: 想要但未调用: serviceService.getServices();

如果我单独运行每个测试或删除“VerificationModeFactory.times(2)”,则一切正常。

这很奇怪。有人可以帮助我吗?

When I put a "VerificationModeFactory.times(2)" in test before, when I run all tests of the class appears this exception:

org.mockito.exceptions.verification.WantedButNotInvoked:
Wanted but not invoked:
serviceService.getServices();

If I run each test separately or remove "VerificationModeFactory.times(2)" all works.

It's very weird. Could anybody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不打扰别人 2024-09-13 18:15:14

看起来好像您的验证是静态声明的 - 每个测试类一次,而不是每个测试方法一次。

这样做:

verify(mock, times(2)).did("my thing");

或者在你的情况下

verify(mockService, times(2)).getServices();

(顺便说一句,你的类获得服务两次重要吗,还是使用它更重要?除非你描述性能修复,否则我可能会专注于服务的价值相反,使用 Mockito 有助于保持测试的灵活性。)

It looks as if your verification is declared statically - once per test class, instead of once per test method.

Do this:

verify(mock, times(2)).did("my thing");

or in your case

verify(mockService, times(2)).getServices();

(BTW, is it important that your class gets the service twice, or is it more important that it used it? Unless you're describing a performance fix, I'd probably concentrate on the value the service provides instead. Using Mockito that way helps keep tests flexible. My 2 cents.)

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