問題描述
我對它們之間的區(qū)別以及在哪種情況下選擇哪個感到困惑.有些區(qū)別可能很明顯,例如 any
和 eq
,但我將它們都包括在內(nèi)只是為了確定.
I am confused on what's the difference between them, and which one to choose in which case. Some difference might be obvious, like any
and eq
, but I'm including them all just to be sure.
我想知道它們的區(qū)別,因?yàn)槲矣龅搅诉@個問題:我在 Controller 類中有這個 POST 方法
I wonder about their differences because I came across this problem: I have this POST method in a Controller class
public Response doSomething(@ResponseBody Request request) {
return someService.doSomething(request);
}
并且想對該控制器執(zhí)行單元測試.我有兩個版本.第一個是簡單的,像這樣
And would like to perform a unit test on that controller. I have two versions. The first one is the simple one, like this
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(req)).thenReturn(res);
Response actualRes = someController.doSomething(req);
assertThat(actualRes, is(res));
}
但我想使用 MockMvc 方法,比如這個
But I wanted to use a MockMvc approach, like this one
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(any(Request.class))).thenReturn(res);
mockMvc.perform(post("/do/something")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(req))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$message", is("done")));
}
兩者都運(yùn)作良好.但我希望我的 someServiceMock.doSomething()
在 MockMvc 方法中接收 req
,或者至少是一個與 req
(不僅僅是任何 Request
類),并返回 res
,就像第一個一樣.我知道使用 MockMvc 方法是不可能的(或者是嗎?),因?yàn)樵趯?shí)際調(diào)用中傳遞的對象總是與在模擬中傳遞的對象不同.無論如何我可以做到這一點(diǎn)嗎?或者這樣做是否有意義?或者我應(yīng)該對使用 any(Request.class)
感到滿意嗎?我試過eq
,same
,但都失敗了.
Both work well. But I wanted my someServiceMock.doSomething()
in the MockMvc approach to receive req
, or at least an object that has the same variable values as req
(not just any Request
class), and return res
, just like the first. I know that it's impossible using the MockMvc approach (or is it?), because the object passed in the actual call is always different from the object passed in the mock. Is there anyway I can achieve that? Or does it even make sense to do that? Or should I be satisfied using any(Request.class)
? I've tried eq
, same
, but all of them fail.
推薦答案
any()
絕對不檢查任何內(nèi)容.從 Mockito 2.0 開始,any(T.class)
共享isA
語義以表示任何T
";或正確地T
類型的任何實(shí)例".any()
checks absolutely nothing. Since Mockito 2.0,any(T.class)
sharesisA
semantics to mean "anyT
" or properly "any instance of typeT
".這是與 Mockito 1.x 相比的一個變化,其中
any(T.class)
完全沒有檢查但在 Java 8 之前保存了一個強(qiáng)制轉(zhuǎn)換:任何類型的對象,對于給定的類不是必需的.提供類參數(shù)只是為了避免強(qiáng)制轉(zhuǎn)換."This is a change compared to Mockito 1.x, where
any(T.class)
checked absolutely nothing but saved a cast prior to Java 8: "Any kind object, not necessary of the given class. The class argument is provided only to avoid casting."isA(T.class)
檢查參數(shù)instanceof T
是否為非空.same(obj)
檢查參數(shù)是否引用與obj
相同的實(shí)例,使得arg == obj
為真.same(obj)
checks that the argument refers to the same instance asobj
, such thatarg == obj
is true.eq(obj)
根據(jù)其equals
方法檢查參數(shù)是否等于obj
.如果您在不使用匹配器的情況下傳遞實(shí)際值,這也是這種行為.eq(obj)
checks that the argument equalsobj
according to itsequals
method. This is also the behavior if you pass in real values without using matchers.請注意,除非
equals
被覆蓋,否則您將看到默認(rèn)的 Object.equals 實(shí)現(xiàn),其行為與same(obj)
相同.Note that unless
equals
is overridden, you'll see the default Object.equals implementation, which would have the same behavior assame(obj)
.如果您需要更精確的自定義,您可以為自己的謂詞使用適配器:
If you need more exact customization, you can use an adapter for your own predicate:
- 對于 Mockito 1.x,請使用
argThat
帶有自定義 HamcrestMatcher
,可以準(zhǔn)確選擇您需要的對象. - 對于 Mockito 2.0 及更高版本,請使用
Matchers.argThat
與自定義org.mockito.ArgumentMatcher
或MockitoHamcrest.argThat
使用自定義 HamcrestMatcher<T>
.
- For Mockito 1.x, use
argThat
with a custom HamcrestMatcher<T>
that selects exactly the objects you need. - For Mockito 2.0 and beyond, use
Matchers.argThat
with a customorg.mockito.ArgumentMatcher<T>
, orMockitoHamcrest.argThat
with a custom HamcrestMatcher<T>
.
您也可以使用
refEq
,它使用reflection來確認(rèn)對象相等;Hamcrest 與公共 bean 的 SamePropertyValuesAs 有類似的實(shí)現(xiàn)-樣式屬性.請注意,在 GitHub issue #1800 上建議棄用和刪除refEq
,并且在那個問題中,您可能更喜歡eq
來更好地為您的類提供更好的封裝,而不是它們的平等感.You may also use
refEq
, which uses reflection to confirm object equality; Hamcrest has a similar implementation with SamePropertyValuesAs for public bean-style properties. Note that on GitHub issue #1800 proposes deprecating and removingrefEq
, and as in that issue you might prefereq
to better give your classes better encapsulation over their sense of equality.這篇關(guān)于Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!- 對于 Mockito 1.x,請使用