pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    • <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="ksa0qi2" 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="cyg0ycs" 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="mccwcim" id='Zx1XO'><tfoot id='Zx1XO'></tfoot><dl id='Zx1XO'><fieldset id='Zx1XO'></fieldset></dl></div>
                            主站蜘蛛池模板: 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 电池高低温试验箱-气态冲击箱-双层电池防爆箱|简户百科 | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 华中线缆有限公司-电缆厂|电缆厂家|电线电缆厂家 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 广东机电安装工程_中央空调工程_东莞装饰装修-广东粤标建设有限公司 | 郑州爱婴幼师学校_专业幼师培训_托育师培训_幼儿教育培训学校 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | 汽车润滑油厂家-机油/润滑油代理-高性能机油-领驰慧润滑科技(河北)有限公司 | 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 | 冷油器-冷油器换管改造-连云港灵动列管式冷油器生产厂家 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 凝胶成像系统(wb成像系统)百科-上海嘉鹏| 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 北京网站建设-企业网站建设-建站公司-做网站-北京良言多米网络公司 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 塑胶地板-商用PVC地板-pvc地板革-安耐宝pvc塑胶地板厂家 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | 江西自考网-江西自学考试网| 原子吸收设备-国产分光光度计-光谱分光光度计-上海光谱仪器有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 一技任务网_有一技之长,就来技术任务网| 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | hdpe土工膜-防渗膜-复合土工膜-长丝土工布价格-厂家直销「恒阳新材料」-山东恒阳新材料有限公司 ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 信阳市建筑勘察设计研究院有限公司 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 服务器之家 - 专注于服务器技术及软件下载分享 | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 |