久久久久久久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 有什么區別?

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

    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 有什么區別?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我對它們之間的區別以及在哪種情況下選擇哪個感到困惑.有些區別可能很明顯,例如 anyeq,但我將它們都包括在內只是為了確定.

                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.

                我想知道它們的區別,因為我遇到了這個問題:我在 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);
                }
                

                并且想對該控制器執行單元測試.我有兩個版本.第一個是簡單的,像這樣

                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")));
                }
                

                兩者都運作良好.但我希望我的 someServiceMock.doSomething() 在 MockMvc 方法中接收 req,或者至少是一個與 req(不僅僅是任何 Request 類),并返回 res,就像第一個一樣.我知道使用 MockMvc 方法是不可能的(或者是嗎?),因為在實際調用中傳遞的對象總是與在模擬中傳遞的對象不同.無論如何我可以做到這一點嗎?或者這樣做是否有意義?或者我應該對使用 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() 絕對不檢查任何內容.從 Mockito 2.0 開始,any(T.class) 共享 isA 語義以表示任何 T";或正確地T 類型的任何實例".

                  • 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 之前保存了一個強制轉換:任何類型的對象,對于給定的類不是必需的.提供類參數只是為了避免強制轉換."

                    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) 檢查參數 instanceof T 是否為非空.

                    same(obj) 檢查參數是否引用與 obj 相同的實例,使得 arg == obj 為真.

                    same(obj) checks that the argument refers to the same instance as obj, such that arg == obj is true.

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

                    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 被覆蓋,否則您將看到默認的 Object.equals 實現,其行為與 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,可以準確選擇您需要的對象.
                    • 對于 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來確認對象相等;Hamcrest 與公共 bean 的 SamePropertyValuesAs 有類似的實現-樣式屬性.請注意,在 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.

                    這篇關于Mockito Matchers isA、any、eq 和 same 有什么區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                How to convert Integer to int?(如何將整數轉換為整數?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 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>
                          主站蜘蛛池模板: 99久久99久久精品国产片果冰 | 在线免费小视频 | 一级做a| 国产精品一区二区三区在线 | 欧美精品在线播放 | 观看av | 国产精品久久久久一区二区 | 亚洲精品视频观看 | 国产黄色在线观看 | 一区二区三区国产精品 | 亚洲欧美中文日韩在线v日本 | 欧美视频在线看 | 亚洲欧美日韩一区 | 国产福利免费视频 | 二区中文字幕 | 精品99爱视频在线观看 | 国外成人在线视频网站 | 国产一区二区三区高清 | 91精品国产欧美一区二区 | 一级片av | 日本在线播放一区二区 | 久久成人亚洲 | 欧美一卡二卡在线观看 | 一区二区三区国产精品 | 欧洲精品在线观看 | 亚洲视频精品在线 | 亚洲一区av在线 | 亚洲区一区二 | 欧美日韩手机在线观看 | 久久综合一区二区三区 | 成人网在线 | 亚洲网站在线播放 | 亚洲不卡| 久久久久久亚洲国产精品 | 欧美日韩一卡二卡 | 国产成人精品一区二区三区在线观看 | 在线免费观看日本视频 | 欧日韩在线观看 | 国产亚洲一区二区精品 | 色视频在线观看 | 欧美日韩国产在线 |