Mockito:想要但未被调用 [英] Mockito: Wanted but not invoked
问题描述
我有以下测试方法:
MyClass myClass= Mockito.mock(MyClass.class);
Mockito.when(myClass.methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class))).thenReturn(Collections.<X, Y> emptyMap());
assertNull(myClass.methodToTest(myObject));
Mockito.verify(myClass).methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class));
methodUsedInMethodBeingTested
是我要模拟并返回空映射的方法.但是我收到的失败消息是
The methodUsedInMethodBeingTested
is a method that I want to mock and return an empty map. But I am getting the failure message saying
想要但未调用myClass.methodUsedInMethodBeingTested()
Wanted but not invoked myClass.methodUsedInMethodBeingTested()
.
MyClass
{
public XYZ methodToTest()
{
....
....
Map<X,Y> mp = methodUsedInMethodBeingTested(myTypeParam);
.....
}
public Map<X,Y> methodUsedInMethodBeingTested(MyTypeParam myTypeParam)
{
.....
}
}
推荐答案
您误解了什么是模拟.做事的时候
You're misunderstanding what a mock is. When you're doing
MyClass myClass = Mockito.mock(MyClass.class);
// ...
assertNull(myClass.methodToTest(myObject));
您实际上并没有在真实对象上调用methodToTest
.您正在模拟上调用methodToTest
,默认情况下,该模拟不执行任何操作并返回null
,除非已将其存根.引用 Mockito文档:
You're not actually invoking methodToTest
on your real object. You're invoking methodToTest
on the mock, which by default, does nothing and return null
, unless it was stubbed. Quoting from Mockito docs:
默认情况下,对于所有返回值的方法,mock返回null,空集合或适当的原始/原始包装器值(例如:0,false,...用于int/Integer,布尔值/布尔值,...) .
By default, for all methods that return value, mock returns null, an empty collection or appropriate primitive/primitive wrapper value (e.g: 0, false, ... for int/Integer, boolean/Boolean, ...).
这说明了您随后出现的错误:该方法实际上并未在模拟中调用.
This explains your subsequent error: the method was really not invoked on the mock.
您似乎想要的是 spy
代替:
It seems what you want here is a spy
instead:
您可以创建真实对象的间谍.当您使用间谍时,将调用 real 方法(除非对某个方法进行了打桩).
You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).
但请注意警告:由于调用的是真实方法,因此您不应使用Mockito.when
,而应使用Mockito.doReturn(...).when
,否则该方法将被真正调用一次.如果考虑表达式:
A note of warning though: since it is the real methods that are getting called, you should not use Mockito.when
but prefer Mockito.doReturn(...).when
, otherwise the method will be called once for real. If you consider the expression:
Mockito.when(myClass.methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class))).thenReturn(Collections.<X, Y> emptyMap());
^-----------------------------------^
this will be invoked by Java
必须评估方法when
的参数,但这意味着将调用方法methodUsedInMethodBeingTested
.由于我们有一个间谍,因此将调用它的真正方法.因此,请改用:
the argument of the method when
must be evaluated, but this means the method methodUsedInMethodBeingTested
will be invoked. And since we have a spy, it is the real method that will be invoked. So, instead, use:
MyClass spy = Mockito.spy(new MyClass());
Mockito.doReturn(Collections.<X, Y> emptyMap()).when(spy).methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class));
assertNull(spy.methodToTest(myObject));
Mockito.verify(spy).methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class));
这篇关于Mockito:想要但未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!