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

    1. <tfoot id='ogdRc'></tfoot>

        <bdo id='ogdRc'></bdo><ul id='ogdRc'></ul>

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

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

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

        Mockito:通緝但未調用

        Mockito: Wanted but not invoked(Mockito:通緝但未調用)

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

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

                  本文介紹了Mockito:通緝但未調用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有如下測試方法:

                  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

                  需要但未調用 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.當你在做的時候

                  You're misunderstanding what a mock is. When you're doing

                  MyClass myClass = Mockito.mock(MyClass.class);
                  // ...
                  assertNull(myClass.methodToTest(myObject));
                  

                  您實際上并沒有在您的真實對象上調用 methodToTest.您正在模擬上調用 methodToTest,默認情況下,它什么也不做并返回 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:

                  默認情況下,對于所有返回值的方法,mock 返回 null、一個空集合或適當的原始/原始包裝值(例如: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, ...).

                  這解釋了您隨后的錯誤:該方法實際上沒有在模擬上調用.

                  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:

                  您可以創建真實對象的間諜.當您使用 spy 時,會調用 real 方法(除非方法被存根).

                  You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

                  警告說明:由于調用的是真正的方法,因此您不應該使用 Mockito.when 而是更喜歡 Mockito.doReturn(...).when,否則該方法將被真正調用一次.如果考慮表達式:

                  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 的參數必須被計算,但這意味著方法 methodUsedInMethodBeingTested 將被調用.因為我們有一個間諜,所以它是真正的方法將被調用.所以,改為使用:

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

                  這篇關于Mockito:通緝但未調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

                  1. <tfoot id='xcVet'></tfoot>

                  2. <small id='xcVet'></small><noframes id='xcVet'>

                    <i id='xcVet'><tr id='xcVet'><dt id='xcVet'><q id='xcVet'><span id='xcVet'><b id='xcVet'><form id='xcVet'><ins id='xcVet'></ins><ul id='xcVet'></ul><sub id='xcVet'></sub></form><legend id='xcVet'></legend><bdo id='xcVet'><pre id='xcVet'><center id='xcVet'></center></pre></bdo></b><th id='xcVet'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='xcVet'><tfoot id='xcVet'></tfoot><dl id='xcVet'><fieldset id='xcVet'></fieldset></dl></div>
                      <tbody id='xcVet'></tbody>
                    • <legend id='xcVet'><style id='xcVet'><dir id='xcVet'><q id='xcVet'></q></dir></style></legend>
                            <bdo id='xcVet'></bdo><ul id='xcVet'></ul>
                            主站蜘蛛池模板: 日韩欧美国产精品 | 成人a视频在线观看 | 欧美日韩三级视频 | 密桃av | 国产美女黄色 | 狠狠干夜夜草 | 亚洲精品乱码久久久久久久久久 | 亚洲男人的天堂网站 | 欧美综合一区二区 | 亚洲欧美精品国产一级在线 | 视频一区二区中文字幕 | h视频在线免费看 | 国产亚洲欧美日韩精品一区二区三区 | 久久久av中文字幕 | 亚洲人成人一区二区在线观看 | 亚洲 欧美 精品 | 久久久123 | 日韩一区二区免费视频 | 国产欧美久久精品 | 亚洲成人精品国产 | 特级黄一级播放 | 亚洲成人在线视频播放 | 中文字幕一区在线观看视频 | 精品一区二区三区中文字幕 | 久久这里只有精品首页 | 韩国精品一区二区三区 | 亚洲免费在线观看视频 | 精品91久久| 精品国产欧美一区二区 | 精品日韩一区 | 欧美精品成人影院 | a免费观看 | 2018中文字幕第一页 | 福利av在线 | 久久这里只有精品首页 | 久久久久久av | 天天摸天天看 | 久久久久久九九九九九九 | 99re在线播放 | 99综合 | 欧美日韩福利视频 |