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

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

        • <bdo id='LqqHU'></bdo><ul id='LqqHU'></ul>
        <legend id='LqqHU'><style id='LqqHU'><dir id='LqqHU'><q id='LqqHU'></q></dir></style></legend>

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

        使用 mockito 模擬使用通配符返回泛型的方法

        mocking a method that return generics with wildcard using mockito(使用 mockito 模擬使用通配符返回泛型的方法)

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

        <tfoot id='BVfxb'></tfoot>

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

              <bdo id='BVfxb'></bdo><ul id='BVfxb'></ul>

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

                    <tbody id='BVfxb'></tbody>
                  本文介紹了使用 mockito 模擬使用通配符返回泛型的方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我使用的是 mockito 1.9.5.我有以下代碼:

                  公共類 ClassA {公共列表getMyInterfaces() {返回空值;}公共靜態(tài)無效testMock(){列出<我的界面>接口 = 新的 ArrayList<>();ClassA classAMock = mock(ClassA.class);when(classAMock.getMyInterfaces()).thenReturn(interfaces);}

                  我得到一個編譯錯誤 thenReturn(interfaces) 說:

                  "類型中的thenReturn(List)方法OngoingStubbing<List<capture#1-of ?擴展我的接口>>不適用于論點(列表<MyInterface>)"

                  但是,當(dāng)我使用 mockito 的 thenAnswer 方法時,我沒有收到錯誤消息.誰能告訴我發(fā)生了什么事?為什么我在使用 thenReturn 方法時會收到錯誤消息?當(dāng) ClassA 由第三方提供且無法修改時,是否有其他方法可以解決此問題?

                  解決方案

                  編輯:從 Mockito 1.10.x 開始,嵌入在類中的泛型類型現(xiàn)在被 Mockito 用于深度存根.IE.

                  公共接口 A<T 擴展 Observer &可比

                  Mockito 盡力獲取編譯器嵌入的類型信息,但是當(dāng)應(yīng)用擦除時,mockito 只能返回 Object 的模擬.

                  <小時>

                  原創(chuàng):嗯,這更多是泛型的問題,而不是 Mockito 的問題.對于泛型,您應(yīng)該閱讀 Angelika Langer 在它們上寫的內(nèi)容.對于當(dāng)前主題,即通配符,請閱讀此部分.p>

                  但簡而言之,您可以使用 Mockito 的其他語法來幫助您解決當(dāng)前的情況:

                  doReturn(interfaces).when(classAMock).getMyInterfaces();

                  或者使用 BDD 別名:

                  willReturn(interfaces).given(classAMock).getMyInterfaces();

                  盡管如此,您可以編寫更通用友好的包裝器.這將有助于未來的開發(fā)人員使用相同的第 3 方 API.

                  <小時>

                  附帶說明:您不應(yīng)該模擬您不擁有的類型,這可能會導(dǎo)致許多錯誤和問題.相反,你應(yīng)該有一些包裝.例如 DAO 和存儲庫就代表了這樣的想法,人們將模擬 DAO 或存儲庫接口,而不是 JDBC/JPA/hibernate 的東西.有很多關(guān)于此的博客文章:

                  • http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html
                  • http://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html
                  • https://web.archive.org/web/20140923101818/http://freshbrewedcode.com/derekgreer/2012/04/01/tdd-best-practices-dont-mock-others/
                  • ...

                  I'm using mockito 1.9.5. I have the following code:

                  public class ClassA  {
                  
                  public List<? extends MyInterface> getMyInterfaces() {
                      return null;
                  }
                  
                  public static void testMock() {
                      List<MyInterface> interfaces = new ArrayList<>();
                      ClassA classAMock = mock(ClassA.class);
                      when(classAMock.getMyInterfaces()).thenReturn(interfaces);      
                  }
                  

                  I get a compilation error for the thenReturn(interfaces) saying:

                  "The method thenReturn(List<capture#1-of ? extends MyInterface>) in the type 
                   OngoingStubbing<List<capture#1-of ? extends MyInterface>> is not applicable for the arguments 
                   (List<MyInterface>)"
                  

                  However, when I use the thenAnswer method of mockito, I don't get the error. Can anyone tell me what's going on? Why do I get the error when I use the thenReturn method? Is there any other way to solve this problem when ClassA is provided by a 3rd party and cannot be modified?

                  解決方案

                  EDIT : Starting from Mockito 1.10.x, generics types that are embedded in the class are now used by Mockito for deep stubs. ie.

                  public interface A<T extends Observer & Comparable<? super T>>  {
                    List<? extends B> bList();
                    T observer();
                  }
                  
                  B b = deep_stubbed.bList().iterator().next(); // returns a mock of B ; mockito remebers that A returns a List of B
                  Observer o = deep_stubbed.observer(); // mockito can find that T super type is Observer
                  Comparable<? super T> c = deep_stubbed.observer(); // or that T implements Comparable
                  

                  Mockito tries its best to get type information that the compiler embeds, but when erasure applies, mockito cannot do anything but return a mock of Object.


                  Original : Well that's more of an issue with generics than with Mockito. For generics, you should read what Angelika Langer wrote on them. And for the current topic, i.e. wildcards, read this section.

                  But for short, what you could use is the other syntax of Mockito to help with your current situation :

                  doReturn(interfaces).when(classAMock).getMyInterfaces();
                  

                  Or with the BDD aliases :

                  willReturn(interfaces).given(classAMock).getMyInterfaces();
                  

                  Nevertheless, you could write wrappers that are more generic friendly. That will help future developers working with same 3rd party API.


                  As a side note: you shouldn't mocks type you don't own, it can lead to many errors and issues. Instead you should have some wrapper. DAO and repositories for example represent such idea, one will mock the DAO or repository interface, but not the JDBC / JPA / hibernate stuff. There are many blog posts about that:

                  • http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html
                  • http://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html
                  • https://web.archive.org/web/20140923101818/http://freshbrewedcode.com/derekgreer/2012/04/01/tdd-best-practices-dont-mock-others/
                  • ...

                  這篇關(guān)于使用 mockito 模擬使用通配符返回泛型的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

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

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

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

                        1. <legend id='LGZ5b'><style id='LGZ5b'><dir id='LGZ5b'><q id='LGZ5b'></q></dir></style></legend>
                          <tfoot id='LGZ5b'></tfoot>
                            <tbody id='LGZ5b'></tbody>
                            主站蜘蛛池模板: 户外健身路径_小区健身器材_室外健身器材厂家_价格-浩然体育 | 螺旋绞龙叶片,螺旋输送机厂家,山东螺旋输送机-淄博长江机械制造有限公司 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 双相钢_双相不锈钢_双相钢圆钢棒_双相不锈钢报价「海新双相钢」 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 科普仪器菏泽市教育教学仪器总厂 | 「银杏树」银杏树行情价格_银杏树种植_山东程锦园林 | 企业彩铃制作_移动、联通、电信集团彩铃上传开通_彩铃定制_商务彩铃管理平台-集团彩铃网 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 化妆品加工厂-化妆品加工-化妆品代加工-面膜加工-广东欧泉生化科技有限公司 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 烟气在线监测系统_烟气在线监测仪_扬尘检测仪_空气质量监测站「山东风途物联网」 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | H型钢切割机,相贯线切割机,数控钻床,数控平面钻,钢结构设备,槽钢切割机,角钢切割机,翻转机,拼焊矫一体机 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 软启动器-上海能曼电气有限公司 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 裹包机|裹膜机|缠膜机|绕膜机-上海晏陵智能设备有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | 番茄畅听邀请码怎么输入 - Dianw8.com | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 稳尚教育加盟-打造高考志愿填报平台_新高考志愿填报加盟_学业生涯规划加盟 | 南昌旅行社_南昌国际旅行社_南昌国旅在线 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 许昌奥仕达自动化设备有限公司 | 100国际学校招生 - 专业国际学校择校升学规划 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 仿真茅草_人造茅草瓦价格_仿真茅草厂家_仿真茅草供应-深圳市科佰工贸有限公司 | 扬州汇丰仪表有限公司| 无机纤维喷涂棉-喷涂棉施工工程-山东华泉建筑工程有限公司▲ | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 科昊仪器超纯水机系统-可成气相液氮罐-美菱超低温冰箱-西安昊兴生物科技有限公司 |