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

  • <small id='XOWc4'></small><noframes id='XOWc4'>

      • <bdo id='XOWc4'></bdo><ul id='XOWc4'></ul>

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

        <legend id='XOWc4'><style id='XOWc4'><dir id='XOWc4'><q id='XOWc4'></q></dir></style></legend>
      2. Mockito:模擬“黑盒";依賴項

        Mockito: Mocking quot;Blackboxquot; Dependencies(Mockito:模擬“黑盒;依賴項)

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

                  <tbody id='r7cZx'></tbody>
                <legend id='r7cZx'><style id='r7cZx'><dir id='r7cZx'><q id='r7cZx'></q></dir></style></legend>

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

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

                • 本文介紹了Mockito:模擬“黑盒";依賴項的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  因此,我被要求為我們的開發團隊閱讀有關 mocking 和 BDD 的內容,并使用 mocks 來改進我們現有的一些單元測試(作為實驗).

                  So I have been asked to read up on mocking and BDD for our development team and play around with mocks so as to improve a handful of our existing unit tests (as an experiment).

                  我最終選擇使用 Mockito 的原因有很多(有些原因超出了我的控制范圍),但就是因為它同時支持存根和模擬,以便在不適合模擬的情況下使用.

                  I have ultimately chosen to go with Mockito for a number of reasons (some outside the scope of my control), but namely because it supports both stubbing and mocking for instances when mocking would not be appropriate.

                  我整天都在學習 Mockito、mock(一般)和 BDD.現在我已準備好深入研究并開始擴充我們的單元測試.

                  I have spent all day learning about Mockito, mocking (in general) and BDD. And now I am ready to dig in and start augmenting our unit tests.

                  所以我們有一個名為 WebAdaptor 的類,它有一個 run() 方法:

                  So we have a class called WebAdaptor that has a run() method:

                  public class WebAdaptor {
                  
                      private Subscriber subscriber;
                  
                      public void run() {
                  
                          subscriber = new Subscriber();
                          subscriber.init();
                      }
                  }
                  

                  請注意:我無法修改此代碼(出于此問題范圍之外的原因!).因此我確實有能力為Subscriber添加一個setter方法,因此它可以被認為是我的WebAdaptor.

                  Please note: I do not have a way to modify this code (for reasons outside the scope of this question!). Thus I do not have the ability to add a setter method for Subscriber, and thus it can be thought of as an unreachable "blackbox" inside of my WebAdaptor.

                  我想編寫一個包含 Mockito 模擬的單元測試,并使用該模擬來 verify 執行 WebAdaptor::run() 導致 Subscriber::init() 被調用.

                  I want to write a unit test which incorporates a Mockito mock, and uses that mock to verify that executing WebAdaptor::run() causes Subscriber::init() to be called.

                  這就是我目前所得到的(在 WebAdaptorUnitTest 內):

                  So here's what I've got so far (inside WebAdaptorUnitTest):

                  @Test
                  public void runShouldInvokeSubscriberInit() {
                  
                      // Given
                      Subscriber mockSubscriber = mock(Subscriber.class);
                      WebAdaptor adaptor = new WebAdaptor();
                  
                      // When
                      adaptor.run();
                  
                      // Then
                      verify(mockSubscriber).init();
                  }
                  

                  當我運行這個測試時,實際的 Subscriber::init() 方法會被執行(我可以從控制臺輸出和本地系統上生成的文件中看出),不是 mockSubscriber,它不應該做(或返回)任何事情.

                  When I run this test, the actual Subscriber::init() method gets executed (I can tell from the console output and seeing files being generated on my local system), not the mockSubscriber, which shouldn't do (or return) anything.

                  我已經檢查并重新檢查:initpublic,既不是 static 也不是 final,并且它返回 void.根據文檔,Mockito 模擬這個對象應該沒有問題.

                  I have checked and re-checked: init is public, is neither static or final, and it returns void. According to the docs, Mockito should have no problem mocking this object.

                  所以我開始思考:我是否需要明確地將 mockSubscriberadaptor 關聯起來?如果是這種情況,那么通常情況下,以下通常會解決它:

                  So it got me thinking: do I need to explictly associate the mockSubscriber with the adaptor? If this is a case, then ordinarily, the following would normally fix it:

                  adaptor.setSubscriber(mockSubscriber);
                  

                  但由于我無法添加任何這樣的設置器(請閱讀我上面的注釋),我不知道如何強制這樣的關聯.所以,幾個非常密切相關的問題:

                  But since I cannot add any such setter (please read my note above), I'm at a loss as to how I could force such an association. So, several very-closely-related questions:

                  • 誰能確認我已正確設置測試(使用 Mockito API)?
                  • 我對失蹤二傳手的懷疑是否正確?(我需要通過 setter 關聯這些對象嗎?)
                  • 如果我的上述懷疑屬實,并且我無法修改 WebAdaptor,是否有任何規避措施可供我使用?
                  • Can anyone confirm that I've set the test up correctly (using the Mockito API)?
                  • Is my suspicion about the missing setter correct? (Do I need to associate these objects via a setter?)
                  • If my above suspicion is true, and I can't modify WebAdaptor, are there any circumventions at my dispose?

                  提前致謝!

                  推薦答案

                  您需要將模擬注入到您正在測試的類中.您不需要訪問訂閱者.mockito 和其他模擬框架的幫助方式是您不需要訪問正在與之交互的對象.但是,您確實需要一種將模擬對象放入您正在測試的類的方法.

                  You need to inject the mock into the class which you are testing. You do not need access to Subscriber. The way mockito and other mocking frameworks help is that you do not need access to objects which you are interacting with. You do however need a way to get mock objects into the class you are testing.

                  public class WebAdaptor {
                  
                      public WebAdaptor(Subscriber subscriber) { /* Added a new constructor */
                         this.subscriber = subscriber;
                      }
                  
                      private Subscriber subscriber;
                  
                      public void run() {
                          subscriber.init();
                      }
                  }
                  

                  現在您可以在模擬對象上驗證您的交互,而不是在真實對象上驗證.

                  Now you can verify your interactions on the mock, rather than on the real object.

                  @Test
                  public void runShouldInvokeSubscriberInit() {
                  
                      // Given
                      Subscriber mockSubscriber = mock(Subscriber.class);
                      WebAdaptor adaptor = new WebAdaptor(mockSubscriber);  // Use the new constructor
                  
                      // When
                      adaptor.run();
                  
                      // Then
                      verify(mockSubscriber).init();
                  }
                  

                  如果將訂閱者添加到構造函數不是正確的方法,您還可以考慮使用工廠來允許 WebAdaptor 從您控制的工廠實例化新訂閱者對象.然后,您可以模擬工廠以提供模擬訂閱者.

                  If adding the Subscriber to the constructor is not the correct approach, you could also consider using a factory to allow WebAdaptor to instantiate new Subscriber objects from a factory which you control. You could then mock the factory to provider mock Subscribers.

                  這篇關于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?)

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

                      <small id='9iBZu'></small><noframes id='9iBZu'>

                          • <bdo id='9iBZu'></bdo><ul id='9iBZu'></ul>

                            主站蜘蛛池模板: 久久久久久久久国产精品 | 国产高清久久久 | 欧美日韩久久精品 | www.免费看片.com | 狠狠操狠狠干 | 日韩欧美视频免费在线观看 | 国产一级片在线观看视频 | 日日干日日射 | 国产高清在线 | 黄色片网站国产 | 亚洲视频在线观看一区二区三区 | 91在线看片 | 一起操网站 | 在线视频一区二区 | 精品欧美黑人一区二区三区 | 日韩欧美精品在线播放 | 夜夜爽99久久国产综合精品女不卡 | 久久久久久亚洲 | 欧美精品一区二区蜜桃 | 1000部精品久久久久久久久 | 亚洲国产精品一区二区久久 | 成人高潮片免费视频欧美 | 一级特黄视频 | 天天射天天操天天干 | 国产一区二区毛片 | 国产伦精品一区二区三毛 | 99热首页| 欧美激情一区二区三区 | 久久久精 | 免费视频一区二区 | 欧美成人a| 国产一区二区三区精品久久久 | 日韩电影免费在线观看中文字幕 | 久久av一区 | 久久久久九九九女人毛片 | 久久在线 | 最近日韩中文字幕 | 亚洲精品一区二三区不卡 | 在线电影日韩 | 久久久区 | 欧美精品1区2区 |