久久久久久久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:通緝但未調(diào)用

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

        • <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:通緝但未調(diào)用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有如下測試方法:

                  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

                  需要但未調(diào)用 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.當(dāng)你在做的時候

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

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

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

                  默認(rèn)情況下,對于所有返回值的方法,mock 返回 null、一個空集合或適當(dāng)?shù)脑?原始包裝值(例如: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, ...).

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

                  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:

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

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

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

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

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

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

                  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>
                            主站蜘蛛池模板: av黄色免费在线观看 | 亚洲风情在线观看 | 欧美日韩中文国产一区发布 | 精品无码久久久久久久动漫 | 国产在线视频一区二区董小宛性色 | 日韩亚洲视频在线 | 午夜国产 | 国产日韩欧美激情 | 精品国产区 | 91最新在线视频 | 视频一区二区中文字幕日韩 | 天天天天天操 | 日韩成人在线观看 | 99精品在线观看 | 中文字幕动漫成人 | 成人在线观看中文字幕 | 综合网中文字幕 | 欧美激情va永久在线播放 | 久久精品小视频 | 91夜夜夜| 国产九九九 | 国产精品久久久久久久久久三级 | 国产美女久久久 | 亚洲精品乱码久久久久久9色 | 国产成人精品久久 | 91视频麻豆 | 免费观看一级视频 | 国产精品一区二区三区四区 | 91亚洲国产成人精品一区二三 | 国产亚洲精品精品国产亚洲综合 | 欧美精品在线看 | 精品欧美乱码久久久久久 | 欧美亚洲激情 | 亚洲一区二区在线视频 | 欧美精品乱码久久久久久按摩 | 久久国产秒 | 精品久久久久久久 | 欧美精品在线免费观看 | 一区二区三区国产在线观看 | 罗宾被扒开腿做同人网站 | 欧美日韩成人在线 |