久久久久久久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免费高清密臂 | 日韩精品a在线观看图片 | www.色53色.com | 国产激情一区二区三区 | 日韩久久久久久久久久久 | 亚洲不卡在线观看 | 人人射人人 | 欧美精品成人一区二区三区四区 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 在线观看日韩精品视频 | 亚洲国产精品第一区二区 | 欧美日韩专区 | 国产精品激情 | 欧美区在线 | 国产欧美视频一区二区三区 | 国产精品99久久久久久动医院 | 特级一级黄色片 | 久久久91精品国产一区二区三区 | 中文字幕 亚洲一区 | 亚洲精品久久久久久久久久久 | 日韩欧美在线不卡 | 欧美成人精品激情在线观看 | 国产精品久久久久永久免费观看 | 国产精品国产三级国产aⅴ中文 | 69亚洲精品| 黄视频网址 | 国产剧情一区 | 国产一区二区久久 | 亚洲成人av在线播放 | 日韩中文字幕在线观看 | 欧美一区日韩一区 | 中文字字幕一区二区三区四区五区 |