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

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

        <tfoot id='inFkZ'></tfoot>

        • <bdo id='inFkZ'></bdo><ul id='inFkZ'></ul>
      1. 當我們無法將模擬對象傳遞給類的實例時如何使

        How to use Mockito when we cannot pass a mock object to an instance of a class(當我們無法將模擬對象傳遞給類的實例時如何使用 Mockito)

          <tbody id='ion2l'></tbody>

          <bdo id='ion2l'></bdo><ul id='ion2l'></ul>
        • <tfoot id='ion2l'></tfoot>
          <legend id='ion2l'><style id='ion2l'><dir id='ion2l'><q id='ion2l'></q></dir></style></legend>

            1. <small id='ion2l'></small><noframes id='ion2l'>

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

                  本文介紹了當我們無法將模擬對象傳遞給類的實例時如何使用 Mockito的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  假設我有這樣的課程:

                  public class MyClass {
                  
                      Dao dao;
                  
                      public String myMethod(Dao d) {
                  
                          dao = d;
                  
                          String result = dao.query();
                  
                          return result;
                      } 
                  }
                  

                  我想用 mockito 測試它.所以我創建了一個模擬對象并以這種方式調用該方法進行測試:

                  I want to test it with mockito. So I create a mock object and I call the method to test in that way:

                  Dao mock = Mockito.mock(Dao.class);
                  
                  Mockito.when(mock.myMethod()).thenReturn("ok");
                  
                  new MyClass().myMethod(mock);
                  

                  但是,假設我有一個這樣的課程:

                  But, suppose instead I have a class like that:

                  public class MyClass {
                  
                      Dao dao = new Dao();
                  
                      public String myMethod() {
                  
                          String result = dao.query();
                  
                          return result;
                      } 
                  }
                  

                  現在我無法將我的模擬作為參數傳遞,那么我將如何測試我的方法?有人可以舉個例子嗎?

                  Now I cannot pass my mock as an argument, so how I gonna test my method? Can someone show an example?

                  推薦答案

                  從根本上說,您試圖用替代實現替換私有字段,這意味著您違反了封裝.您唯一的其他選擇是重組類或方法,使其更適合測試.

                  Fundamentally, you're trying to replace a private field with an alternative implementation, which means you'd violate encapsulation. Your only other option is to restructure the class or method, to make it better-designed for testing.

                  評論中有很多簡短的答案,所以我在這里將它們匯總(并添加我自己的幾個)作為社區 Wiki.如果您有任何替代方案,請隨時在此處添加.

                  There are a lot of short answers in the comments, so I'm aggregating them here (and adding a couple of my own) as Community Wiki. If you have any alternatives, please feel free to add them here.

                  • 為相關字段創建一個 setter,或放寬該字段的可見性.

                  • Create a setter for the field in question, or relax the field's visibility.

                  創建一個采用 DAO 的依賴注入覆蓋或靜態方法,并將公共實例方法委托給它.改為測試更靈活的方法.

                  Create a dependency-injecting override or static method that takes a DAO, and make the public instance method delegate to it. Test the more-flexible method instead.

                  public String myMethod() { return myMethod(dao); }
                  String myMethod(Dao dao) { /* real implementation here */ }
                  

                1. 添加構造函數重載或靜態工廠方法來替換私有字段以進行測試.

                2. Add a constructor overload or static factory method that replaces private fields for the sake of testing.

                  完全構建依賴注入的類.(Sotirios Delimanolis, EJK)

                  Fully structure the class for dependency injection. (Sotirios Delimanolis, EJK)

                  請注意,如果您將測試放在同一個 Java 包中(可能在單獨的源代碼樹中),其中一些可以是包私有的以進行測試.在任何情況下,良好的名稱和文檔都有助于明確您的意圖.

                  Note that some of these can be package-private for testing, if you put your tests in the same Java package (possibly in a separate source tree). In all cases, good names and documentation are helpful to make your intentions clear.

                  • 使用反射在類中設置私有字段.(kinbiko - 請參閱 answer)
                  • 使用 PowerMockito 替換 Dao 構造函數與您選擇的模擬.(戴夫·牛頓)
                  • Use reflection to set private fields in the class. (kinbiko - see answer)
                  • Use PowerMockito to replace the Dao constructor with a mock of your choice. (Dave Newton)

                  這篇關于當我們無法將模擬對象傳遞給類的實例時如何使用 Mockito的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  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?)
                  <i id='lRhuh'><tr id='lRhuh'><dt id='lRhuh'><q id='lRhuh'><span id='lRhuh'><b id='lRhuh'><form id='lRhuh'><ins id='lRhuh'></ins><ul id='lRhuh'></ul><sub id='lRhuh'></sub></form><legend id='lRhuh'></legend><bdo id='lRhuh'><pre id='lRhuh'><center id='lRhuh'></center></pre></bdo></b><th id='lRhuh'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='lRhuh'><tfoot id='lRhuh'></tfoot><dl id='lRhuh'><fieldset id='lRhuh'></fieldset></dl></div>

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

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

                          • 主站蜘蛛池模板: 日本精品久久 | 午夜精品一区 | 奇米av | 日韩免费一二三区 | 国产大片黄色 | 久久精品国产久精国产 | 久草免费在线视频 | 国产精品一区在线 | 日韩av.com | 91免费在线| 中文在线日韩 | 久久综合伊人一区二区三 | 精品一区二区三区四区在线 | 国产一区二区 | 欧美一级免费观看 | 日本精品一区 | 国产一级淫片免费视频 | 老司机精品福利视频 | 九九免费观看视频 | 视频在线观看一区二区 | av在线免费观看网站 | 91精品久久| 国产精品久久久久久久久免费 | 日韩一区二区三区在线观看 | 免费观看一级特黄欧美大片 | 亚洲高清视频在线 | 在线观看免费黄色片 | 国产精品美女久久久久aⅴ国产馆 | 日本午夜在线视频 | 免费一区二区 | 亚洲精品女优 | 国产人成精品一区二区三 | 国产成人在线一区二区 | 亚洲午夜精品一区二区三区他趣 | 精品一区二区三区av | 国产成人a亚洲精品 | 蜜桃视频在线观看免费视频网站www | 成人av鲁丝片一区二区小说 | 欧美精品在欧美一区二区 | 欧美午夜视频 | 亚洲成人网在线 |