久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <legend id='PgzHN'><style id='PgzHN'><dir id='PgzHN'><q id='PgzHN'></q></dir></style></legend>

      <bdo id='PgzHN'></bdo><ul id='PgzHN'></ul>
  1. <i id='PgzHN'><tr id='PgzHN'><dt id='PgzHN'><q id='PgzHN'><span id='PgzHN'><b id='PgzHN'><form id='PgzHN'><ins id='PgzHN'></ins><ul id='PgzHN'></ul><sub id='PgzHN'></sub></form><legend id='PgzHN'></legend><bdo id='PgzHN'><pre id='PgzHN'><center id='PgzHN'></center></pre></bdo></b><th id='PgzHN'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='PgzHN'><tfoot id='PgzHN'></tfoot><dl id='PgzHN'><fieldset id='PgzHN'></fieldset></dl></div>
    <tfoot id='PgzHN'></tfoot>

      <small id='PgzHN'></small><noframes id='PgzHN'>

    1. Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?

      What#39;s the difference between Mockito Matchers isA, any, eq, and same?(Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?)

    2. <tfoot id='sy7xv'></tfoot>

          <tbody id='sy7xv'></tbody>
          <legend id='sy7xv'><style id='sy7xv'><dir id='sy7xv'><q id='sy7xv'></q></dir></style></legend>

            <small id='sy7xv'></small><noframes id='sy7xv'>

              <bdo id='sy7xv'></bdo><ul id='sy7xv'></ul>
            • <i id='sy7xv'><tr id='sy7xv'><dt id='sy7xv'><q id='sy7xv'><span id='sy7xv'><b id='sy7xv'><form id='sy7xv'><ins id='sy7xv'></ins><ul id='sy7xv'></ul><sub id='sy7xv'></sub></form><legend id='sy7xv'></legend><bdo id='sy7xv'><pre id='sy7xv'><center id='sy7xv'></center></pre></bdo></b><th id='sy7xv'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sy7xv'><tfoot id='sy7xv'></tfoot><dl id='sy7xv'><fieldset id='sy7xv'></fieldset></dl></div>

              • 本文介紹了Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我對它們之間的區(qū)別以及在哪種情況下選擇哪個感到困惑.有些區(qū)別可能很明顯,例如 anyeq,但我將它們都包括在內(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) 感到滿意嗎?我試過eqsame,但都失敗了.

                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) shares isA semantics to mean "any T" or properly "any instance of type T".

                    這是與 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 as obj, such that arg == obj is true.

                    eq(obj) 根據(jù)其 equals 方法檢查參數(shù)是否等于 obj.如果您在不使用匹配器的情況下傳遞實(shí)際值,這也是這種行為.

                    eq(obj) checks that the argument equals obj according to its equals 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 as same(obj).

                    如果您需要更精確的自定義,您可以為自己的謂詞使用適配器:

                    If you need more exact customization, you can use an adapter for your own predicate:

                    • 對于 Mockito 1.x,請使用 argThat 帶有自定義 Hamcrest Matcher,可以準(zhǔn)確選擇您需要的對象.
                    • 對于 Mockito 2.0 及更高版本,請使用 Matchers.argThat 與自定義 org.mockito.ArgumentMatcherMockitoHamcrest.argThat使用自定義 Hamcrest Matcher<T>.
                    • For Mockito 1.x, use argThat with a custom Hamcrest Matcher<T> that selects exactly the objects you need.
                    • For Mockito 2.0 and beyond, use Matchers.argThat with a custom org.mockito.ArgumentMatcher<T>, or MockitoHamcrest.argThat with a custom Hamcrest Matcher<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 removing refEq, and as in that issue you might prefer eq 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)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(guān)系嗎?)
                How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個隨機(jī)打亂數(shù)字的 int 數(shù)組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)

                    <tfoot id='YXsmr'></tfoot>
                      <tbody id='YXsmr'></tbody>
                      <bdo id='YXsmr'></bdo><ul id='YXsmr'></ul>
                      <legend id='YXsmr'><style id='YXsmr'><dir id='YXsmr'><q id='YXsmr'></q></dir></style></legend>

                        <small id='YXsmr'></small><noframes id='YXsmr'>

                        • <i id='YXsmr'><tr id='YXsmr'><dt id='YXsmr'><q id='YXsmr'><span id='YXsmr'><b id='YXsmr'><form id='YXsmr'><ins id='YXsmr'></ins><ul id='YXsmr'></ul><sub id='YXsmr'></sub></form><legend id='YXsmr'></legend><bdo id='YXsmr'><pre id='YXsmr'><center id='YXsmr'></center></pre></bdo></b><th id='YXsmr'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='YXsmr'><tfoot id='YXsmr'></tfoot><dl id='YXsmr'><fieldset id='YXsmr'></fieldset></dl></div>
                          主站蜘蛛池模板: 国产精品欧美一区喷水 | 亚洲综合二区 | 免费观看国产视频在线 | 国产欧美精品一区二区 | 超碰在线97国产 | 国产精品久久精品 | 天天操操 | 范冰冰一级做a爰片久久毛片 | 粉嫩av久久一区二区三区 | 一本综合久久 | 亚洲天堂中文字幕 | 天天操天天玩 | 国产成人久久精品一区二区三区 | 亚洲品质自拍视频 | 小草久久久久久久久爱六 | 亚州春色 | 嫩草91在线| 羞视频在线观看 | 男女深夜网站 | 久久日韩粉嫩一区二区三区 | 国产欧美一区二区三区免费 | 国产综合av | 日本精品一区二区三区在线观看视频 | 一级黄色片免费在线观看 | 久久精品一区 | 国精产品一区二区三区 | 亚洲精品中文字幕 | 午夜小视频在线观看 | 欧美激情精品久久久久久变态 | 欧美精品在线免费 | 色一情一乱一伦一区二区三区 | 蜜桃黄网| 在线免费观看黄色av | 色在线免费视频 | 一区二区免费高清视频 | 久久久91精品国产一区二区三区 | 中文字幕在线第一页 | 天天艹天天干天天 | 在线观看国产网站 | 男女啪啪网址 | 亚洲精品视频久久 |