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="77ftxnp" 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="rv7hprf" 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="tddv5z7" id='FOcR7'><tfoot id='FOcR7'></tfoot><dl id='FOcR7'><fieldset id='FOcR7'></fieldset></dl></div>
                            主站蜘蛛池模板: 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 哈尔滨发电机,黑龙江柴油发电机组-北方星光 | 领先的大模型技术与应用公司-中关村科金| 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 | 软文世界-软文推广-软文营销-新闻稿发布-一站式软文自助发稿平台 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 大学食堂装修设计_公司餐厅效果图_工厂食堂改造_迈普装饰 | 气体热式流量计-定量控制流量计(空气流量计厂家)-湖北南控仪表科技有限公司 | 热处理炉-退火炉-回火炉设备厂家-丹阳市电炉厂有限公司 | 博客-悦享汽车品质生活 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 紫外可见光分光度计-紫外分光度计-分光光度仪-屹谱仪器制造(上海)有限公司 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 橡胶接头_橡胶软接头_可曲挠橡胶接头-巩义市创伟机械制造有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 直流大电流电源,燃料电池检漏设备-上海政飞 | 实验室装修_实验室设计_实验室规划设计- 上海广建净化工程公司 | 上海质量认证办理中心| SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | 次氯酸钠厂家,涉水级次氯酸钠,三氯化铁生产厂家-淄博吉灿化工 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 学校用栓剂模,玻璃瓶轧盖钳,小型安瓿熔封机,实验室安瓿熔封机-长沙中亚制药设备有限公司 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 旋转气浴恒温振荡器-往复式水浴恒温振荡器-金怡百科 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 消泡剂-水处理消泡剂-涂料消泡剂-切削液消泡剂价格-东莞德丰消泡剂厂家 |