久久久久久久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>

        如何驗證靜態(tài) void 方法是否已使用 power mockito 調(diào)

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

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

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

                  問題描述

                  我正在使用以下內(nèi)容.

                  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,但是根據(jù)code,如果下單成功則必須發(fā)送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() 之類的東西),真的不需要調(diào)用 驗證*().也就是說,這是我重寫測試方法的嘗試:

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

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

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

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

                  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.

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

                  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.

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

                  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.

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

                  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.

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

                  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)

                  這篇關(guān)于如何驗證靜態(tài) void 方法是否已使用 power 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)建一個隨機打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?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>
                            主站蜘蛛池模板: 在线国产一区二区 | 日韩成人免费视频 | 国产一区二区三区高清 | 免费激情网站 | 午夜视频一区二区 | 久久av一区二区三区 | 国产精品成人一区 | 国产精品久久久久久中文字 | 欧美一区二区在线免费观看 | 日本91av视频 | 欧美不卡一区二区三区 | 欧美一区二区另类 | 狠狠涩 | 久久精品亚洲 | 91视频在线 | 91精品国产色综合久久 | 日本在线视频不卡 | 日本粉嫩一区二区三区视频 | 欧美性久久 | 久久久久久国产精品三区 | 国产成人亚洲精品自产在线 | 久久专区 | 亚洲三区在线观看 | 99精品欧美一区二区蜜桃免费 | 日韩成人在线观看 | 精品三区 | 久久国产视频网 | 欧美一级在线 | 国产一区二区三区精品久久久 | 一本一道久久a久久精品综合蜜臀 | 日韩欧美久久精品 | 91在线网站 | 成人国产精品久久久 | 一区二区三区精品 | 成人a在线观看 | 国产精品久久久久久久一区探花 | 九一视频在线播放 | 国产精品99久久久精品免费观看 | 久久91精品国产一区二区 | 少妇淫片aaaaa毛片叫床爽 | 中文字幕国产 |