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="820ywsc" 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 模擬新對象的構(gòu)造?)
          <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="wiwomq0" 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 模擬新對象的構(gòu)造?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我想編寫一個 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 的調(diào)用引發(fā)異常并顯示以下消息:

                  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.
                  

                  我應(yīng)該使用什么來代替 PowerMocktio.spy 來設(shè)置對 whenNew 的調(diào)用?

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

                  推薦答案

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

                  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的匿名類,當(dāng)編譯器看到一個匿名類時,它會創(chuàng)建一個finalpackage visible類.所以匿名類不能通過標(biāo)準(zhǔn)方法模擬,即通過 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.

                  好的,現(xiàn)在假設(shè)您希望能夠通過 Powermock 模擬這個匿名類.當(dāng)前編譯器使用以下方案編譯匿名類:

                  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>
                  

                  模擬匿名類可能但很脆弱(我是認(rèn)真的)所以假設(shè)匿名類是第 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
                  

                  所以你可以準(zhǔn)備測試匿名類:

                  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.

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

                  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 位數(shù)字:

                  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>
                  

                  另外,我認(rèn)為 JLS 并沒有明確規(guī)定編譯器應(yīng)該如何命名匿名類.

                  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 的值.所以你需要通過反射來實際設(shè)置這個字段.您可以使用 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();
                  }
                  

                  現(xiàn)在是過濾器本身:

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

                  現(xiàn)在你可以編寫這樣的測試了:

                  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 意味著設(shè)計有問題.在我看來,Powermock 非常適合遺留軟件,但在設(shè)計新代碼時會使開發(fā)人員失明;由于他們錯過了 OOP 的優(yōu)點,我什至?xí)f他們正在設(shè)計遺留代碼.

                  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.

                  希望有幫助!

                  這篇關(guān)于在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構(gòu)造?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='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="comwmy0" id='Mhh02'><tfoot id='Mhh02'></tfoot><dl id='Mhh02'><fieldset id='Mhh02'></fieldset></dl></div>
                          • <bdo id='Mhh02'></bdo><ul id='Mhh02'></ul>
                            主站蜘蛛池模板: 步进_伺服_行星减速机,微型直流电机,大功率直流电机-淄博冠意传动机械 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 亚克力制品定制,上海嘉定有机玻璃加工制作生产厂家—官网 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 微动开关厂家-东莞市德沃电子科技有限公司 | IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 加盟店-品牌招商加盟-创业项目商机平台 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 汽液过滤网厂家_安平县银锐丝网有限公司 | sfp光模块,高速万兆光模块工厂-性价比更高的光纤模块制造商-武汉恒泰通 | 动环监控_机房环境监控_DCIM_机房漏水检测-斯特纽 | 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 铝扣板-铝方通-铝格栅-铝条扣板-铝单板幕墙-佳得利吊顶天花厂家 elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 代写标书-专业代做标书-商业计划书代写「深圳卓越创兴公司」 | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | 翰香原枣子坊加盟费多少钱-正宗枣核糕配方培训利润高飘香 | 胶辊硫化罐_胶鞋硫化罐_硫化罐厂家-山东鑫泰鑫智能装备有限公司 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 长城人品牌官网 | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 三轴曲线机-端子插拔力试验机|华杰仪器| 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 四川职高信息网-初高中、大专、职业技术学校招生信息网 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 水性绝缘漆_凡立水_绝缘漆树脂_环保绝缘漆-深圳维特利环保材料有限公司 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 保定市泰宏机械制造厂-河北铸件厂-铸造厂-铸件加工-河北大件加工 | 旗帜网络笔记-免费领取《旗帜网络笔记》电子书 | 佛山市德信昌电子有限公司| 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 信阳市建筑勘察设计研究院有限公司 | 专业的压球机生产线及解决方案厂家-河南腾达机械厂 | 圆形振动筛_圆筛_旋振筛_三次元振动筛-河南新乡德诚生产厂家 |