【发布时间】:2019-02-01 19:37:47
【问题描述】:
问题:
假设我们有两个不同的服务
class ServiceA(serviceB: ServiceB) {
def methodA(): String = {
"methodA called"
serviceB.methodB()
}
def methodA2(): String = {
"methodA2 called"
serviceB.methodB()
}
}
我编写了两个测试方法并尝试验证是否调用了methodB。当我分别运行两个测试用例时,测试通过了。当我运行所有测试方法时,验证给出了第二次测试的错误结果。 在测试类上下文中,mockito-verification 记录对模拟对象的所有调用。我认为 mockito-scala 应该在每次测试后重置模拟对象的验证计数
class ServiceATest extends FlatSpec with IdiomaticMockito {
val serviceB: ServiceB = mock[ServiceB]
val serviceA: ServiceA = new ServiceA(serviceB)
"methodA" should "called" in {
serviceA.methodA()
serviceB.methodB wasCalled once // Passes
}
"methodA2" should "called" in {
serviceA.methodA2()
serviceB.methodB wasCalled once // Fail. was 2 times
}
}
顺便说一句,我尝试了 mockito-core,它给出了同样的错误。
【问题讨论】: