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

    • <bdo id='Pl8sf'></bdo><ul id='Pl8sf'></ul>
  • <small id='Pl8sf'></small><noframes id='Pl8sf'>

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

        如何驗證靜態 void 方法是否已使用 power mockito 調

        How to verify static void method has been called with power mockito(如何驗證靜態 void 方法是否已使用 power mockito 調用)

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

            <tbody id='Y4AgG'></tbody>

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

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

                  本文介紹了如何驗證靜態 void 方法是否已使用 power mockito 調用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在使用以下內容.

                  Powermock-mockito 1.5.12
                  Mockito 1.95
                  junit 4.11
                  

                  這是我的實用程序類

                  public void InternalUtils {
                      public static void sendEmail(String from, String[] to, String msg, String body) {
                      }
                  }
                  

                  這里是被測類的要點:

                  public class InternalService {
                         public void processOrder(Order order) {
                             if (order.isSuccessful()) {
                                 InternalUtils.sendEmail(...);
                             }
                         }
                  }
                  

                  這是測試:

                  @PrepareForTest({InternalUtils.class})
                  @RunWith(PowerMockRunner.class)
                  public class InternalService {
                     public void verifyEmailSend() {
                          mockStatic(Internalutils.class);
                          doNothing().when(InternalUtils, "sendEmail", anyString(), any(String.class), anyString(), anyString());
                          Order order = mock(Order.class);
                          when(order.isSuccessful()).thenReturn(true);
                          InternalService is = new InternalService();
                  
                          verifyStatic(times(1));
                          is.processOrder(order);
                     }
                  }
                  

                  上述測試失敗.給定的驗證方式是none,但是根據code,如果下單成功則必須發送email.

                  The above test fails. The verification mode given is none, but according to the code, if order is successful than email must be send.

                  推薦答案

                  如果你在模擬行為(使用 doNothing() 之類的東西),真的不需要調用 驗證*().也就是說,這是我重寫測試方法的嘗試:

                  If you are mocking the behavior (with something like doNothing()) there should really be no need to call to verify*(). That said, here's my stab at re-writing your test method:

                  @PrepareForTest({InternalUtils.class})
                  @RunWith(PowerMockRunner.class)
                  public class InternalServiceTest { //Note the renaming of the test class.
                     public void testProcessOrder() {
                          //Variables
                          InternalService is = new InternalService();
                          Order order = mock(Order.class);
                  
                          //Mock Behavior
                          when(order.isSuccessful()).thenReturn(true);
                          mockStatic(Internalutils.class);
                          doNothing().when(InternalUtils.class); //This is the preferred way
                                                                 //to mock static void methods.
                          InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
                  
                          //Execute
                          is.processOrder(order);            
                  
                          //Verify
                          verifyStatic(InternalUtils.class); //Similar to how you mock static methods
                                                             //this is how you verify them.
                          InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
                     }
                  }
                  

                  為了更好地突出正在發生的事情,我將其分為四個部分:

                  I grouped into four sections to better highlight what is going on:

                  我選擇在這里聲明任何實例變量/方法參數/模擬協作者.如果它在多個測試中使用,請考慮將其作為測試類的實例變量.

                  I choose to declare any instance variables / method arguments / mock collaborators here. If it is something used in multiple tests, consider making it an instance variable of the test class.

                  這是您定義所有模擬行為的地方.在執行被測代碼之前,您在此處設置返回值和期望值.一般來說,如果您在此處設置模擬行為,則以后無需驗證該行為.

                  This is where you define the behavior of all of your mocks. You're setting up return values and expectations here, prior to executing the code under test. Generally speaking, if you set the mock behavior here you wouldn't need to verify the behavior later.

                  這里沒有什么花哨的;這只是啟動正在測試的代碼.我喜歡給它一個單獨的部分來引起人們的注意.

                  Nothing fancy here; this just kicks off the code being tested. I like to give it its own section to call attention to it.

                  這是當您調用任何以 verifyassert 開頭的方法時.測試結束后,您檢查您希望發生的事情是否確實發生了.這是我在您的測試方法中看到的最大錯誤;你試圖在它有機會運行之前驗證方法調用.其次是您從未指定哪個要驗證的靜態方法.

                  This is when you call any method starting with verify or assert. After the test is over, you check that the things you wanted to have happen actually did happen. That is the biggest mistake I see with your test method; you attempted to verify the method call before it was ever given a chance to run. Second to that is you never specified which static method you wanted to verify.

                  這主要是我個人的喜好.您需要按照一定的順序做事,但在每個分組中都有一點回旋余地.這有助于我快速區分出發生了什么.

                  This is mostly personal preference on my part. There is a certain order you need to do things in but within each grouping there is a little wiggle room. This helps me quickly separate out what is happening where.

                  我還強烈建議您瀏覽以下網站上的示例,因為它們非常強大,可以幫助您處理您需要的大多數案例:

                  I also highly recommend going through the examples at the following sites as they are very robust and can help with the majority of the cases you'll need:

                  • https://github.com/powermock/powermock/wiki/Mockito (PowerMock 概述/示例)
                  • http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html(Mockito 概述/示例)
                  • https://github.com/powermock/powermock/wiki/Mockito (PowerMock Overview / Examples)
                  • http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html (Mockito Overview / Examples)

                  這篇關于如何驗證靜態 void 方法是否已使用 power 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?)

                  • <tfoot id='Zx1XO'></tfoot>

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

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

                            <legend id='Zx1XO'><style id='Zx1XO'><dir id='Zx1XO'><q id='Zx1XO'></q></dir></style></legend>
                              <tbody id='Zx1XO'></tbody>
                            <i id='Zx1XO'><tr id='Zx1XO'><dt id='Zx1XO'><q id='Zx1XO'><span id='Zx1XO'><b id='Zx1XO'><form id='Zx1XO'><ins id='Zx1XO'></ins><ul id='Zx1XO'></ul><sub id='Zx1XO'></sub></form><legend id='Zx1XO'></legend><bdo id='Zx1XO'><pre id='Zx1XO'><center id='Zx1XO'></center></pre></bdo></b><th id='Zx1XO'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Zx1XO'><tfoot id='Zx1XO'></tfoot><dl id='Zx1XO'><fieldset id='Zx1XO'></fieldset></dl></div>
                            主站蜘蛛池模板: 日韩一区中文字幕 | 一区在线播放 | 亚洲视频区| 精品国产一区二区三区久久狼黑人 | 中文字幕一区二区三区四区五区 | 男女视频在线免费观看 | 日本三级在线 | h视频在线免费看 | 在线视频一区二区三区 | 成人亚洲视频 | 亚洲精品中文字幕中文字幕 | 在线日韩中文字幕 | 欧美精品一区二区三区在线四季 | 中文字幕在线观看 | 色久电影 | 国产精品久久久久久久久久久久 | 亚洲精品天堂 | 欧美99 | av免费网站在线观看 | 国产精品久久久久一区二区三区 | 午夜精品一区二区三区在线视 | 亚洲一区二区av | 在线观看成人 | 男女视频在线观看网站 | 成人精品一区亚洲午夜久久久 | 国精产品一区一区三区免费完 | 手机av免费在线 | 国内久久精品 | 欧美a v在线 | 在线看av的网址 | 久久精品久久综合 | 国产精品久久久久久福利一牛影视 | 精品国产18久久久久久二百 | 中文在线一区二区 | 一区二区日韩 | 免费午夜视频在线观看 | 亚洲视频在线免费 | 毛片网站免费观看 | 欧美一级久久 | 亚洲欧美激情视频 | 国产精品无码久久久久 |