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

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

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

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

        在匿名類中測試方法時,如何使用 Powermockito 模擬

        How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?(在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構造?)
          <tbody id='rk5Yf'></tbody>
          <i id='rk5Yf'><tr id='rk5Yf'><dt id='rk5Yf'><q id='rk5Yf'><span id='rk5Yf'><b id='rk5Yf'><form id='rk5Yf'><ins id='rk5Yf'></ins><ul id='rk5Yf'></ul><sub id='rk5Yf'></sub></form><legend id='rk5Yf'></legend><bdo id='rk5Yf'><pre id='rk5Yf'><center id='rk5Yf'></center></pre></bdo></b><th id='rk5Yf'></th></span></q></dt></tr></i><div class="pzlxbz7" id='rk5Yf'><tfoot id='rk5Yf'></tfoot><dl id='rk5Yf'><fieldset id='rk5Yf'></fieldset></dl></div>

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

              <tfoot id='rk5Yf'></tfoot>
            1. <legend id='rk5Yf'><style id='rk5Yf'><dir id='rk5Yf'><q id='rk5Yf'></q></dir></style></legend>
                  <bdo id='rk5Yf'></bdo><ul id='rk5Yf'></ul>

                  本文介紹了在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構造?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想編寫一個 JUnit 測試來驗證下面的代碼是否使用了 BufferedInputStream:

                  I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream:

                  public static final FilterFactory BZIP2_FACTORY = new FilterFactory() {
                      public InputStream makeFilter(InputStream in) {        
                          // a lot of other code removed for clarity 
                          BufferedInputStream buffer = new BufferedInputStream(in);
                          return new CBZip2InputStream(buffer);
                      }
                  };
                  

                  (FilterFactory 是一個接口.)

                  (FilterFactory is an interface.)

                  到目前為止,我的測試如下所示:

                  My test thus far looks like this:

                  @Test
                  public void testBZIP2_FactoryUsesBufferedInputStream() throws Throwable {
                      InputStream in = mock(InputStream.class);
                      BufferedInputStream buffer = mock(BufferedInputStream.class);
                      CBZip2InputStream expected = mock(CBZip2InputStream.class);
                  
                      PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
                      whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
                      whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
                      InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);
                  
                      assertEquals(expected, observed);
                  }
                  

                  對 PowerMockito.spy 的調用引發異常并顯示以下消息:

                  The call to PowerMockito.spy raises an exception with this message:

                  org.mockito.exceptions.base.MockitoException: 
                  Mockito cannot mock this class: class edu.gvsu.cis.kurmasz.io.InputHelper$1
                  Mockito can only mock visible & non-final classes.
                  

                  我應該使用什么來代替 PowerMocktio.spy 來設置對 whenNew 的調用?

                  What should I be using instead of PowerMocktio.spy to set up the calls to whenNew?

                  推薦答案

                  信息很明顯:你不能模擬非可見類和 final 類.簡短回答:為您的匿名類創建一個命名類,然后測試這個類

                  The message is pretty obvious: You can't mock non-visible and final classes. Short answer : Create a named class of your anonymous one, and test this class instead!

                  長答案,讓我們來挖掘一下原因!

                  Long answer, let's dig why !

                  你實例化一個FilterFactory的匿名類,當編譯器看到一個匿名類時,它會創建一個finalpackage visible類.所以匿名類不能通過標準方法模擬,即通過 Mockito.

                  You instantiate an anonymous class of FilterFactory, when the compiler sees an anonymous class, it creates a final and package visible class. So the anonymous class is not mockable through standard mean i.e. through Mockito.

                  好的,現在假設您希望能夠通過 Powermock 模擬這個匿名類.當前編譯器使用以下方案編譯匿名類:

                  OK, now suppose you want to be able to mock this anonymous class through Powermock. Current compilers compile anonymous class with following scheme :

                  Declaring class + $ + <order of declaration starting with 1>
                  

                  模擬匿名類可能但很脆弱(我是認真的)所以假設匿名類是第 11 個被聲明的類,它會顯示為

                  Mocking anonymous class possible but brittle (And I mean it) So supposing the anonymous class is the eleventh to be declared, it will appear as

                  InputHelper$11.class
                  

                  所以你可以準備測試匿名類:

                  So you could potentially prepare for test the anonymous class:

                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest({InputHelper$11.class})
                  public class InputHelperTest {
                      @Test
                      public void anonymous_class_mocking works() throws Throwable {
                          PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
                      }
                  }
                  

                  此代碼將編譯,但最終會在您的 IDE 中報告為錯誤.IDE 可能不知道 InputHelper$11.class.不使用編譯類的IntelliJ檢查代碼報告如此.

                  This code will compile, BUT will eventually be reported as an error with your IDE. The IDE probably doesn't know about InputHelper$11.class. IntelliJ who doesn't use compiled class to check the code report so.

                  匿名類命名實際上取決于聲明的順序這一事實也是一個問題,當有人之前添加另一個匿名類時,編號可能會改變.匿名類是為了保持匿名,如果編譯器決定有一天使用字母甚至隨機標識符怎么辦!

                  Also the fact that the anonymous class naming actually depends on the order of the declaration is a problem, when someone adds another anonymous class before, the numbering could change. Anonymous classes are made to stay anonymous, what if the compiler guys decide one day to use letters or even random identifiers!

                  所以通過 Powermock 模擬匿名類是可能的,但很脆弱,千萬不要在實際項目中這樣做!

                  編輯說明: Eclipse 編譯器有不同的編號方案,它總是使用 3 位數字:

                  EDITED NOTE : The Eclipse compiler has a different numbering scheme, it always uses a 3 digit number :

                  Declaring class + $ + <pad with 0> + <order of declaration starting with 1>
                  

                  另外,我認為 JLS 并沒有明確規定編譯器應該如何命名匿名類.

                  Also I don't think the JLS clearly specify how the compilers should name anonymous classes.

                  PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
                  whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
                  whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
                  InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);
                  

                  PowerMockito.spy 返回 spy,它不會改變 InputHelper.BZIP2_FACTORY 的值.所以你需要通過反射來實際設置這個字段.您可以使用 Powermock 提供的 Whitebox 實用程序.

                  PowerMockito.spy returns the spy, it doesn't change the value of InputHelper.BZIP2_FACTORY. So you would need to actually set via reflection this field. You can use the Whiteboxutility that Powermock provide.

                  只用模擬來測試匿名過濾器使用 BufferedInputStream 太麻煩了.

                  Too much trouble to just test with mocks that the anonymous filter uses a BufferedInputStream.

                  我寧愿寫如下代碼:

                  一個將使用命名類的輸入助手,我不使用接口名稱向用戶說明此過濾器的意圖是什么!

                  An input helper that will use the named class, I don't use the interface name to make clear to the user what is the intent of this filter!

                  public class InputHelper {
                      public static final BufferedBZIP2FilterFactory BZIP2_FACTORY = new BufferedBZIP2FilterFactory();
                  }
                  

                  現在是過濾器本身:

                  public class BufferedBZIP2FilterFactory {
                      public InputStream makeFilter(InputStream in) {
                          BufferedInputStream buffer = new BufferedInputStream(in);
                          return new CBZip2InputStream(buffer);
                      }
                  }
                  

                  現在你可以編寫這樣的測試了:

                  Now you can write a test like this :

                  @RunWith(PowerMockRunner.class)
                  public class BufferedBZIP2FilterFactoryTest {
                  
                      @Test
                      @PrepareForTest({BufferedBZIP2FilterFactory.class})
                      public void wraps_InputStream_in_BufferedInputStream() throws Exception {
                          whenNew(CBZip2InputStream.class).withArguments(isA(BufferedInputStream.class))
                                  .thenReturn(Mockito.mock(CBZip2InputStream.class));
                  
                          new BufferedBZIP2FilterFactory().makeFilter(anInputStream());
                  
                          verifyNew(CBZip2InputStream.class).withArguments(isA(BufferedInputStream.class));
                      }
                  
                      private ByteArrayInputStream anInputStream() {
                          return new ByteArrayInputStream(new byte[10]);
                      }
                  }
                  

                  但如果你強制 CBZip2InputStream 只接受 BufferedInputStream,最終可以避免這個測試場景的 powermock 東西.通常使用 Powermock 意味著設計有問題.在我看來,Powermock 非常適合遺留軟件,但在設計新代碼時會使開發人員失明;由于他們錯過了 OOP 的優點,我什至會說他們正在設計遺留代碼.

                  But could eventually avoid powermock stuff for this test scenario if you force the CBZip2InputStream to only accept BufferedInputStream. Usually using Powermock means something is wrong with the design. In my opinion Powermock is great for legacy softwares, but can blind developers when designing new code; as they are missing the point of OOP's good part, I would even say they are designing legacy code.

                  希望有幫助!

                  這篇關于在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構造?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='Mhh02'></tfoot><legend id='Mhh02'><style id='Mhh02'><dir id='Mhh02'><q id='Mhh02'></q></dir></style></legend>

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

                          <tbody id='Mhh02'></tbody>

                          <i id='Mhh02'><tr id='Mhh02'><dt id='Mhh02'><q id='Mhh02'><span id='Mhh02'><b id='Mhh02'><form id='Mhh02'><ins id='Mhh02'></ins><ul id='Mhh02'></ul><sub id='Mhh02'></sub></form><legend id='Mhh02'></legend><bdo id='Mhh02'><pre id='Mhh02'><center id='Mhh02'></center></pre></bdo></b><th id='Mhh02'></th></span></q></dt></tr></i><div class="jhhhp7v" id='Mhh02'><tfoot id='Mhh02'></tfoot><dl id='Mhh02'><fieldset id='Mhh02'></fieldset></dl></div>
                          • <bdo id='Mhh02'></bdo><ul id='Mhh02'></ul>
                            主站蜘蛛池模板: 查分易-成绩发送平台官网| 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 迪威娱乐|迪威娱乐客服|18183620002| 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 芝麻黑-芝麻黑石材厂家-永峰石业 | 生鲜配送系统-蔬菜食材配送管理系统-连锁餐饮订货配送软件-挪挪生鲜供应链管理软件 | 东莞工作服_东莞工作服定制_工衣订做_东莞厂服| 精密线材测试仪-电线电缆检测仪-苏州欣硕电子科技有限公司 | QQ房产导航-免费收录优秀房地产网站_房地产信息网 | 媒介云-全网整合营销_成都新闻媒体发稿_软文发布平台 | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 超声波_清洗机_超声波清洗机专业生产厂家-深圳市好顺超声设备有限公司 | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 挤奶设备过滤纸,牛奶过滤纸,挤奶机过滤袋-济南蓝贝尔工贸有限公司 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 开云(中国)Kaiyun·官方网站-登录入口| 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 水轮机密封网 | 水轮机密封产品研发生产厂家 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | 二手注塑机回收_旧注塑机回收_二手注塑机买卖 - 大鑫二手注塑机 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 国际船舶网 - 船厂、船舶、造船、船舶设备、航运及海洋工程等相关行业综合信息平台 | 岛津二手液相色谱仪,岛津10A液相,安捷伦二手液相,安捷伦1100液相-杭州森尼欧科学仪器有限公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 乐考网-银行从业_基金从业资格考试_初级/中级会计报名时间_中级经济师 | 杭州代理记账多少钱-注册公司代办-公司注销流程及费用-杭州福道财务管理咨询有限公司 | 烽火安全网_加密软件、神盾软件官网 | 艾乐贝拉细胞研究中心 | 国家组织工程种子细胞库华南分库 | 培训一点通 - 合肥驾校 - 合肥新亚驾校 - 合肥八一驾校 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 |