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

    <legend id='inFkZ'><style id='inFkZ'><dir id='inFkZ'><q id='inFkZ'></q></dir></style></legend>
  • <i id='inFkZ'><tr id='inFkZ'><dt id='inFkZ'><q id='inFkZ'><span id='inFkZ'><b id='inFkZ'><form id='inFkZ'><ins id='inFkZ'></ins><ul id='inFkZ'></ul><sub id='inFkZ'></sub></form><legend id='inFkZ'></legend><bdo id='inFkZ'><pre id='inFkZ'><center id='inFkZ'></center></pre></bdo></b><th id='inFkZ'></th></span></q></dt></tr></i><div class="5ptjbxz" id='inFkZ'><tfoot id='inFkZ'></tfoot><dl id='inFkZ'><fieldset id='inFkZ'></fieldset></dl></div>
    1. <small id='inFkZ'></small><noframes id='inFkZ'>

        <tfoot id='inFkZ'></tfoot>

        • <bdo id='inFkZ'></bdo><ul id='inFkZ'></ul>
      1. 當我們無法將模擬對象傳遞給類的實例時如何使

        How to use Mockito when we cannot pass a mock object to an instance of a class(當我們無法將模擬對象傳遞給類的實例時如何使用 Mockito)

          <tbody id='ion2l'></tbody>

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

            1. <small id='ion2l'></small><noframes id='ion2l'>

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

                  本文介紹了當我們無法將模擬對象傳遞給類的實例時如何使用 Mockito的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  假設我有這樣的課程:

                  public class MyClass {
                  
                      Dao dao;
                  
                      public String myMethod(Dao d) {
                  
                          dao = d;
                  
                          String result = dao.query();
                  
                          return result;
                      } 
                  }
                  

                  我想用 mockito 測試它.所以我創建了一個模擬對象并以這種方式調用該方法進行測試:

                  I want to test it with mockito. So I create a mock object and I call the method to test in that way:

                  Dao mock = Mockito.mock(Dao.class);
                  
                  Mockito.when(mock.myMethod()).thenReturn("ok");
                  
                  new MyClass().myMethod(mock);
                  

                  但是,假設我有一個這樣的課程:

                  But, suppose instead I have a class like that:

                  public class MyClass {
                  
                      Dao dao = new Dao();
                  
                      public String myMethod() {
                  
                          String result = dao.query();
                  
                          return result;
                      } 
                  }
                  

                  現在我無法將我的模擬作為參數傳遞,那么我將如何測試我的方法?有人可以舉個例子嗎?

                  Now I cannot pass my mock as an argument, so how I gonna test my method? Can someone show an example?

                  推薦答案

                  從根本上說,您試圖用替代實現替換私有字段,這意味著您違反了封裝.您唯一的其他選擇是重組類或方法,使其更適合測試.

                  Fundamentally, you're trying to replace a private field with an alternative implementation, which means you'd violate encapsulation. Your only other option is to restructure the class or method, to make it better-designed for testing.

                  評論中有很多簡短的答案,所以我在這里將它們匯總(并添加我自己的幾個)作為社區 Wiki.如果您有任何替代方案,請隨時在此處添加.

                  There are a lot of short answers in the comments, so I'm aggregating them here (and adding a couple of my own) as Community Wiki. If you have any alternatives, please feel free to add them here.

                  • 為相關字段創建一個 setter,或放寬該字段的可見性.

                  • Create a setter for the field in question, or relax the field's visibility.

                  創建一個采用 DAO 的依賴注入覆蓋或靜態方法,并將公共實例方法委托給它.改為測試更靈活的方法.

                  Create a dependency-injecting override or static method that takes a DAO, and make the public instance method delegate to it. Test the more-flexible method instead.

                  public String myMethod() { return myMethod(dao); }
                  String myMethod(Dao dao) { /* real implementation here */ }
                  

                1. 添加構造函數重載或靜態工廠方法來替換私有字段以進行測試.

                2. Add a constructor overload or static factory method that replaces private fields for the sake of testing.

                  完全構建依賴注入的類.(Sotirios Delimanolis, EJK)

                  Fully structure the class for dependency injection. (Sotirios Delimanolis, EJK)

                  請注意,如果您將測試放在同一個 Java 包中(可能在單獨的源代碼樹中),其中一些可以是包私有的以進行測試.在任何情況下,良好的名稱和文檔都有助于明確您的意圖.

                  Note that some of these can be package-private for testing, if you put your tests in the same Java package (possibly in a separate source tree). In all cases, good names and documentation are helpful to make your intentions clear.

                  • 使用反射在類中設置私有字段.(kinbiko - 請參閱 answer)
                  • 使用 PowerMockito 替換 Dao 構造函數與您選擇的模擬.(戴夫·牛頓)
                  • Use reflection to set private fields in the class. (kinbiko - see answer)
                  • Use PowerMockito to replace the Dao constructor with a mock of your choice. (Dave Newton)

                  這篇關于當我們無法將模擬對象傳遞給類的實例時如何使用 Mockito的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
                3. 相關文檔推薦

                  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?)
                  <i id='lRhuh'><tr id='lRhuh'><dt id='lRhuh'><q id='lRhuh'><span id='lRhuh'><b id='lRhuh'><form id='lRhuh'><ins id='lRhuh'></ins><ul id='lRhuh'></ul><sub id='lRhuh'></sub></form><legend id='lRhuh'></legend><bdo id='lRhuh'><pre id='lRhuh'><center id='lRhuh'></center></pre></bdo></b><th id='lRhuh'></th></span></q></dt></tr></i><div class="lpzjhnd" id='lRhuh'><tfoot id='lRhuh'></tfoot><dl id='lRhuh'><fieldset id='lRhuh'></fieldset></dl></div>

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

                        <tbody id='lRhuh'></tbody>
                        • <bdo id='lRhuh'></bdo><ul id='lRhuh'></ul>
                          <tfoot id='lRhuh'></tfoot>
                          <legend id='lRhuh'><style id='lRhuh'><dir id='lRhuh'><q id='lRhuh'></q></dir></style></legend>

                          • 主站蜘蛛池模板: 海水晶,海水素,海水晶价格-潍坊滨海经济开发区强隆海水晶厂 | 德国进口电锅炉_商用电热水器_壁挂炉_电采暖器_电热锅炉[德国宝] | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 宠物店加盟_宠物连锁店_开宠物店-【派多格宠物】 | 学考网学历中心| 天然气分析仪-液化气二甲醚分析仪|传昊仪器 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 北京成考网-北京成人高考网| 世纪豪门官网 世纪豪门集成吊顶加盟电话 世纪豪门售后电话 | 西宁装修_西宁装修公司-西宁业之峰装饰-青海业之峰墅级装饰设计公司【官网】 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 2025第九届世界无人机大会 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 盘古网络技术有限公司| 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | 台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 上海刑事律师|刑事辩护律师|专业刑事犯罪辩护律师免费咨询-[尤辰荣]金牌上海刑事律师团队 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 深圳市东信高科自动化设备有限公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | 模切之家-专注服务模切行业的B2B平台!| 氨水-液氨-工业氨水-氨水生产厂家-辽宁顺程化工 | 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | 间苯二酚,间苯二酚厂家-淄博双和化工 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 钢托盘,钢制托盘,立库钢托盘,金属托盘制造商_南京飞天金属制品实业有限公司 | 天津中都白癜风医院_天津白癜风医院_天津治疗白癜风 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 塑钢件_塑钢门窗配件_塑钢配件厂家-文安县启泰金属制品有限公司 深圳南财多媒体有限公司介绍 |