問(wèn)題描述
我遇到了 Mockito 和 Hamcrest 的泛型問(wèn)題.
I'm running into a generics problem with Mockito and Hamcrest.
請(qǐng)假設(shè)如下界面:
public interface Service {
void perform(Collection<String> elements);
}
還有下面的測(cè)試片段:
Service service = mock(Service.class);
// ... perform business logic
verify(service).perform(Matchers.argThat(contains("a", "b")));
所以我想驗(yàn)證我的業(yè)務(wù)邏輯是否真的使用包含a"和b"的集合來(lái)調(diào)用服務(wù).
So I want to verify that my business logic actually called the service with a collection that contains "a" and "b" in that order.
但是contains(...)
的返回類型是Matcher
Matchers.argThat(...)
在我的情況下返回 Iterable<String>
,這自然不適用于所需集合
.
However, the return type of contains(...)
is Matcher<Iterable<? extends E>>
, so Matchers.argThat(...)
returns Iterable<String>
in my case, which naturally does not apply to the required Collection<String>
.
我知道我可以使用 Hamcrest hasItem and Mockito verify 中建議的參數(shù)捕獲器不一致,但我非常不想這樣做.
I know that I could use an argument captor as proposed in Hamcrest hasItem and Mockito verify inconsistency, but I would very much like not to.
任何建議!謝謝!
推薦答案
你可以寫(xiě)
verify(service).perform((Collection<String>) Matchers.argThat(contains("a", "b")));
從編譯器的角度來(lái)看,這是將 Iterable<String>
轉(zhuǎn)換為 Collection
argThat
將返回 null
,因此可以在沒(méi)有 ClassCastException
的情況下將其傳遞給 perform
.重要的一點(diǎn)是,匹配器進(jìn)入 Mockito 的內(nèi)部參數(shù)結(jié)構(gòu)進(jìn)行驗(yàn)證,這就是 argThat
所做的.
From the compiler's point of view, this is casting an Iterable<String>
to a Collection<String>
which is fine, because the latter is a subtype of the former. At run time, argThat
will return null
, so that can be passed to perform
without a ClassCastException
. The important point about it is that the matcher gets onto Mockito's internal structure of arguments for verification, which is what argThat
does.
這篇關(guān)于Mockito 和 Hamcrest:如何驗(yàn)證 Collection 參數(shù)的調(diào)用?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!