Mockito 的问题 - excepts.verification.WantedButNotInvoked
当我之前在测试中放置“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来好像您的验证是静态声明的 - 每个测试类一次,而不是每个测试方法一次。
这样做:
或者在你的情况下
(顺便说一句,你的类获得服务两次重要吗,还是使用它更重要?除非你描述性能修复,否则我可能会专注于服务的价值相反,使用 Mockito 有助于保持测试的灵活性。)
It looks as if your verification is declared statically - once per test class, instead of once per test method.
Do this:
or in your case
(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.)