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

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

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

        我在 Mockito 2.2 中使用什么代替 Whitebox 來設置字段

        What do I use instead of Whitebox in Mockito 2.2 to set fields?(我在 Mockito 2.2 中使用什么代替 Whitebox 來設置字段?)
          • <bdo id='UaOy4'></bdo><ul id='UaOy4'></ul>
            <i id='UaOy4'><tr id='UaOy4'><dt id='UaOy4'><q id='UaOy4'><span id='UaOy4'><b id='UaOy4'><form id='UaOy4'><ins id='UaOy4'></ins><ul id='UaOy4'></ul><sub id='UaOy4'></sub></form><legend id='UaOy4'></legend><bdo id='UaOy4'><pre id='UaOy4'><center id='UaOy4'></center></pre></bdo></b><th id='UaOy4'></th></span></q></dt></tr></i><div class="mukse2u" id='UaOy4'><tfoot id='UaOy4'></tfoot><dl id='UaOy4'><fieldset id='UaOy4'></fieldset></dl></div>

                  <tbody id='UaOy4'></tbody>
              1. <small id='UaOy4'></small><noframes id='UaOy4'>

                <tfoot id='UaOy4'></tfoot>

                <legend id='UaOy4'><style id='UaOy4'><dir id='UaOy4'><q id='UaOy4'></q></dir></style></legend>
                • 本文介紹了我在 Mockito 2.2 中使用什么代替 Whitebox 來設置字段?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  在使用 Mockito 1.9.x 時,我一直在使用 Whitebox 將字段值設置為注入"模擬.請參閱下面的示例:

                  @Before公共無效設置(){eventHandler = new ProcessEventHandler();securityService = new SecurityServiceMock();注冊服務 = 模擬(注冊服務.class);Whitebox.setInternalState(eventHandler, "registrationService", registrationService);Whitebox.setInternalState(eventHandler, "securityService", securityService);}

                  我真的很喜歡這種方法,但現在我嘗試升級到 Mockito 2.2.7 我注意到(或者更確切地說,我的 IDE 注意到并告訴了我很多次),在 Mockito 中不再可以找到 Whitebox.

                  我找到了一種替代方案,可以作為替代品,那就是 org.powermock.reflect.Whitebox,問題是我得到了另一個依賴項(Powermock),只是為了使用 Whitebox.

                  Powermock 也有一個名為 Whitebox 的類,但不幸的是它看起來好像不能與 Mockito 2.2.x 一起使用p>

                  既然 Whitebox 不再可用,我可以使用 Mockito 中的任何好的替代方法來手動注入"字段嗎?

                  <小時>

                  解決方案

                  我在評論中回復@JeffBowman 的帖子.簡而言之,我選擇復制 WhiteBox 的代碼并使用它,因為它在大多數測試用例中使用,并且該類與其他類沒有依賴關系.這是解決此問題的最快途徑.

                  注意 @bcody 建議的解決方案是一個更好的選擇,如果您使用的是 spring,它不需要額外的代碼供您維護.我很晚才得到這些信息:(

                  解決方案

                  請注意,Whitebox 始終位于 org.mockito.internal 包中.除了主要版本號的遞增之外,internal 名稱表明該軟件包可能會受到重大更改.

                  如果您確實想在測試中設置其他無法訪問的字段,您可以像 setInternalState 一樣進行設置,這只是在層次結構,在其上調用 setAccessible ,然后設置它.完整代碼在 grepcode 上. 您還可以查看許多 在測試中設置不可訪問狀態的其他方法.

                  public static void setInternalState(Object target, String field, Object value) {類<?>c = 目標.getClass();嘗試 {字段 f = getFieldFromHierarchy(c, field);//檢查超類.f.setAccessible(true);f.set(目標,值);} 捕捉(異常 e){拋出新的運行時異常(無法在私有字段上設置內部狀態.[...]",e);}}

                  然而,在這種情況下,我的一般建議是停止使用工具:Java 的四個封裝級別(公共、受保護、包、私有)不是必須足夠細化以表達您嘗試表達的保護程度,并且通常更容易添加一個記錄良好的初始化方法或構造函數覆蓋來覆蓋依賴關系,因為您正在嘗試進行反思.如果您將測試與其測試的類放在同一個 Java 包中,您通常甚至可以將字段或方法/構造函數設置為包私有,這也是設置并行源文件夾 srctests (etc) 代表同一個 Java 包的兩半.

                  雖然有些人將這種額外的方法或構造函數視為API 污染",但我認為它是針對您的類中最重要的消費者之一(它自己的測試)的要求進行的編碼.如果您需要一個原始的外部接口,您可以輕松地單獨定義一個,這樣您就可以隱藏您想要的任何細節.但是,您可能會發現您喜歡將任何真實或模擬實現直接注入到您現在更靈活的組件中的能力,此時您可能需要研究依賴注入模式或框架.p>

                  When using Mockito 1.9.x I have been using Whiteboxto set values of fields to "inject" mocks. Se example below:

                  @Before
                  public void setUp() {
                  
                      eventHandler = new ProcessEventHandler();
                      securityService = new SecurityServiceMock();
                      registrationService = mock(RegistrationService.class);
                  
                      Whitebox.setInternalState(eventHandler, "registrationService", registrationService);
                      Whitebox.setInternalState(eventHandler, "securityService", securityService);
                  }
                  

                  I really like this approach, but now that I tried to upgrade to Mockito 2.2.7 I noticed (or rather, my IDE noticed and told me quite a few times) that Whitebox was no longer to be found in Mockito.

                  I have found one alternative, that can work as a replacement, and that is org.powermock.reflect.Whitebox, the problem with that is that I get another dependency (Powermock), just to use Whitebox.

                  Powermock also have a class named Whitebox, but unfortunately it looks as if it can not be used with Mockito 2.2.x

                  Is there any good alternatives in Mockito that I can use to manually "inject" fields, now that Whitebox is no longer available?


                  Solution

                  I wrote in a comment in response to the post made of @JeffBowman. In short I chose to copy the code of WhiteBox, and use that, since it is used in most of the test cases and the class does not have dependencies to other classes. It was the fastest path to solve this issue.

                  Note The solution that @bcody suggest is a better alternative, if you are using spring, it ads no extra code for you to maintain. I got that information to late :(

                  解決方案

                  Note that Whitebox was always in the org.mockito.internal package. Beyond the incrementing of the major version number, the internal designation is a giveaway that the package may be subject to breaking changes.

                  If you do want to make it a point to set otherwise-inaccessible fields in your test, you can do so in the same way that setInternalState does, which is just to identify the field in the hierarchy, call setAccessible on it, and then set it. The full code is here on grepcode. You can also examine a number of other ways to set inaccessible state in tests.

                  public static void setInternalState(Object target, String field, Object value) {
                      Class<?> c = target.getClass();
                      try {
                          Field f = getFieldFromHierarchy(c, field);  // Checks superclasses.
                          f.setAccessible(true);
                          f.set(target, value);
                      } catch (Exception e) {
                          throw new RuntimeException(
                              "Unable to set internal state on a private field. [...]", e);
                      }
                  }
                  

                  However, in situations like this, my general advice is to stop fighting the tools: Java's four levels of encapsulation (public, protected, package, private) are not necessarily granular enough to express the degree of protection you're trying to express, and it's often much easier to add a well-documented initialization method or constructor override to override the dependencies as you're trying to do reflectively. If you put your tests in the same Java package as the class it tests, you can often even make the fields or method/constructor package-private, which is also a good reason to set up parallel source folders src and tests (etc) that represent two halves of the same Java package.

                  Though some treat this additional method or constructor as "API pollution", I see it instead as coding to the requirements of one of your class's most important consumers—its own test. If you need a pristine external interface, you can easily define one separately such that you can hide any details you'd like. However, you may find you like the ability to inject any real or mock implementation directly into your now-more-flexible component, at which point you may want to look into dependency injection patterns or frameworks.

                  這篇關于我在 Mockito 2.2 中使用什么代替 Whitebox 來設置字段?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)
                    <tbody id='FOcR7'></tbody>

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

                    <bdo id='FOcR7'></bdo><ul id='FOcR7'></ul>
                    <tfoot id='FOcR7'></tfoot>

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

                      <i id='FOcR7'><tr id='FOcR7'><dt id='FOcR7'><q id='FOcR7'><span id='FOcR7'><b id='FOcR7'><form id='FOcR7'><ins id='FOcR7'></ins><ul id='FOcR7'></ul><sub id='FOcR7'></sub></form><legend id='FOcR7'></legend><bdo id='FOcR7'><pre id='FOcR7'><center id='FOcR7'></center></pre></bdo></b><th id='FOcR7'></th></span></q></dt></tr></i><div class="6ocysq2" id='FOcR7'><tfoot id='FOcR7'></tfoot><dl id='FOcR7'><fieldset id='FOcR7'></fieldset></dl></div>
                            主站蜘蛛池模板: 屏蔽泵厂家,化工屏蔽泵_维修-淄博泵业 | 无刷电机_直流无刷电机_行星减速机-佛山市藤尺机电设备有限公司 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 深圳诚暄fpc首页-柔性线路板,fpc柔性线路板打样生产厂家 | 西子馋火锅鸡加盟-太原市龙城酉鼎餐饮管理有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | 日本东丽膜_反渗透膜_RO膜价格_超滤膜_纳滤膜-北京东丽阳光官网 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 深圳活动策划公司|庆典策划|专业公关活动策划|深圳艺典文化传媒 重庆中专|职高|技校招生-重庆中专招生网 | 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 土壤肥料养分速测仪_测土配方施肥仪_土壤养分检测仪-杭州鸣辉科技有限公司 | 飞飞影视_热门电影在线观看_影视大全 | 电磁流量计厂家_涡街流量计厂家_热式气体流量计-青天伟业仪器仪表有限公司 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 电动葫芦|手拉葫芦|环链电动葫芦|微型电动葫芦-北京市凌鹰起重机械有限公司 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 环氧树脂地坪漆_济宁市新天地漆业有限公司 | 防渗膜厂家|养殖防渗膜|水产养殖防渗膜-泰安佳路通工程材料有限公司 | 中国玩具展_玩具展|幼教用品展|幼教展|幼教装备展 | 破碎机锤头_耐磨锤头_合金锤头-鼎成机械一站式耐磨铸件定制服务 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 宁夏活性炭_防护活性炭_催化剂载体炭-宁夏恒辉活性炭有限公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 重庆LED显示屏_显示屏安装公司_重庆LED显示屏批发-彩光科技公司 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 | 螺杆式冷水机-低温冷水机厂家-冷冻机-风冷式-水冷式冷水机-上海祝松机械有限公司 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 杭州实验室尾气处理_实验台_实验室家具_杭州秋叶实验设备有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 首页|光催化反应器_平行反应仪_光化学反应仪-北京普林塞斯科技有限公司 | 塑料薄膜_PP薄膜_聚乙烯薄膜-常州市鑫美新材料包装厂 | 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 食安观察网| 植筋胶-粘钢胶-碳纤维布-碳纤维板-环氧砂浆-加固材料生产厂家-上海巧力建筑科技有限公司 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 |