問題描述
我有如下測試方法:
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
需要但未調(diào)用 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)
{
.....
}
}
推薦答案
你誤解了什么是 mock.當(dāng)你在做的時候
You're misunderstanding what a mock is. When you're doing
MyClass myClass = Mockito.mock(MyClass.class);
// ...
assertNull(myClass.methodToTest(myObject));
您實際上并沒有在您的真實對象上調(diào)用 methodToTest
.您正在模擬上調(diào)用 methodToTest
,默認(rèn)情況下,它什么也不做并返回 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:
默認(rèn)情況下,對于所有返回值的方法,mock 返回 null、一個空集合或適當(dāng)?shù)脑?原始包裝值(例如:0、false、...對于 int/Integer、boolean/Boolean、...).
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, ...).
這解釋了您隨后的錯誤:該方法實際上沒有在模擬上調(diào)用.
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:
您可以創(chuàng)建真實對象的間諜.當(dāng)您使用 spy 時,會調(diào)用 real 方法(除非方法被存根).
You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).
警告說明:由于調(diào)用的是真正的方法,因此您不應(yīng)該使用 Mockito.when
而是更喜歡 Mockito.doReturn(...).when
,否則該方法將被真正調(diào)用一次.如果考慮表達(dá)式:
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
的參數(shù)必須被計算,但這意味著方法 methodUsedInMethodBeingTested
將被調(diào)用.因為我們有一個間諜,所以它是真正的方法將被調(diào)用.所以,改為使用:
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));
這篇關(guān)于Mockito:通緝但未調(diào)用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!