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

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

<tfoot id='zEcQh'></tfoot>

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

          <bdo id='zEcQh'></bdo><ul id='zEcQh'></ul>
      2. 為什么在 Java 中使用靜態輔助方法不好?

        Why is using static helper methods in Java bad?(為什么在 Java 中使用靜態輔助方法不好?)
          <tbody id='0HKgh'></tbody>
          1. <small id='0HKgh'></small><noframes id='0HKgh'>

              <bdo id='0HKgh'></bdo><ul id='0HKgh'></ul>
              <tfoot id='0HKgh'></tfoot><legend id='0HKgh'><style id='0HKgh'><dir id='0HKgh'><q id='0HKgh'></q></dir></style></legend>
            • <i id='0HKgh'><tr id='0HKgh'><dt id='0HKgh'><q id='0HKgh'><span id='0HKgh'><b id='0HKgh'><form id='0HKgh'><ins id='0HKgh'></ins><ul id='0HKgh'></ul><sub id='0HKgh'></sub></form><legend id='0HKgh'></legend><bdo id='0HKgh'><pre id='0HKgh'><center id='0HKgh'></center></pre></bdo></b><th id='0HKgh'></th></span></q></dt></tr></i><div class="3382qdu" id='0HKgh'><tfoot id='0HKgh'></tfoot><dl id='0HKgh'><fieldset id='0HKgh'></fieldset></dl></div>

                1. 本文介紹了為什么在 Java 中使用靜態輔助方法不好?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我之所以問,是因為我正在嘗試使用一個不允許您模擬靜態方法的模擬框架 (Mockito).調查它,我發現很多博客文章說你應該盡可能少地使用靜態方法,但我很難理解為什么.特別是為什么不修改全局狀態并且基本上是輔助方法的方法.例如,我有一個名為 ApiCaller 的類,它有幾個靜態方法.靜態方法的目的之一是執行 HTTP 調用,處理我們的服務器可能返回的任何自定義問題(例如用戶未登錄)并返回響應.為了簡化,類似:

                  I'm asking because I'm trying to use a mocking framework (Mockito) which does not allow you to mock static methods. Looking into it I've found quite a few blog posts saying that you should have as few static methods as possible, but I'm having difficulty wrapping my head around why. Specifically why methods that don't modify the global state and are basically helper methods. For instance I have a class called ApiCaller that has several static methods. One of the static method's purpose is to execute an HTTP call, deal with any custom issues our server might have returned (ex. user not logged in) and return the response. To simplify, something like:

                  public class ApiCaller {
                  ...
                     public static String makeHttpCall(Url url) {
                          // Performs logic to retrieve response and deal with custom server errors
                          ...
                          return response;
                     }
                  }
                  

                  要使用它,我只需調用 ApiCaller.makeHttpCall(url)現在我可以輕松地將其設為非靜態方法,例如:

                  To use this all I have to do is call ApiCaller.makeHttpCall(url) Now I could easily make this a non static method like:

                  public class ApiCaller {
                  ...
                     public String makeHttpCall(Url url) {
                          // Performs logic to retrieve response and deal with custom server errors
                          ...
                          return response;
                     }
                  }
                  

                  然后使用此方法調用 new ApiCaller().makeHttpCall() 但這似乎是額外的開銷.誰能解釋為什么這很糟糕,以及是否有更好的解決方案使方法成為非靜態方法(除了刪除關鍵字之外),以便我可以使用模擬框架刪除這些方法?

                  and then to use this method call new ApiCaller().makeHttpCall() but this just seems like extra overhead. Can anyone explain why this is bad and if there is a better solution to making the methods non static (other than just removing the keyword) so that I can stub out these methods using the mocking framework?

                  謝謝!

                  推薦答案

                  靜態方法的問題是當它們與您嘗試測試的系統無關時,它們很難偽造.想象一下這段代碼:

                  The problem with static methods is they're very hard to fake when they're not relevant to the system you're trying to test. Imagine this code:

                  public void systemUnderTest() {
                      Log.connectToDatabaseForAuditing();
                      doLogicYouWantToTest();
                  }
                  

                  connectToDatabaseForAuditing() 方法是靜態的.您不在乎此方法對您要編寫的測試有什么作用.但是,現在要測試此代碼,您需要一個可用的數據庫.

                  The connectToDatabaseForAuditing() method is static. You don't care what this method does for the test you want to write. But, to test this code now you need an available database.

                  如果它不是靜態的,代碼將如下所示:

                  If it were not static the code would look like this:

                  private Logger log; //instantiate in a setter AKA dependency injection/inversion of control
                  
                  public void systemUnderTest() {
                      log.connectToDatabaseForAuditing();
                      doLogicYouWantToTest();
                  }
                  

                  如果現在沒有數據庫,您的測試將是微不足道的:

                  And your test would be trivial to write without a database now:

                  @Before
                  public void setUp() {
                      YourClass yourClass = new YourClass();
                      yourClass.setLog(new NoOpLogger());
                  
                  }
                  
                  //.. your tests
                  

                  想象一下當方法是靜態的時嘗試這樣做.除了修改記錄器以擁有一個名為 inTestMode 的靜態變量,您在 setUp() 中將其設置為 true 以確保它不會'不連接到數據庫.

                  Imagine trying to do that when the method is static. I can't really think of a way except for modifying the logger to have a static variable called inTestMode that you set to true in the setUp() to make sure it doesn't connect to a database.

                  這篇關于為什么在 Java 中使用靜態輔助方法不好?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)
                  • <bdo id='3oKC2'></bdo><ul id='3oKC2'></ul>

                      <small id='3oKC2'></small><noframes id='3oKC2'>

                        <tbody id='3oKC2'></tbody>

                        <legend id='3oKC2'><style id='3oKC2'><dir id='3oKC2'><q id='3oKC2'></q></dir></style></legend>

                          <tfoot id='3oKC2'></tfoot>

                            <i id='3oKC2'><tr id='3oKC2'><dt id='3oKC2'><q id='3oKC2'><span id='3oKC2'><b id='3oKC2'><form id='3oKC2'><ins id='3oKC2'></ins><ul id='3oKC2'></ul><sub id='3oKC2'></sub></form><legend id='3oKC2'></legend><bdo id='3oKC2'><pre id='3oKC2'><center id='3oKC2'></center></pre></bdo></b><th id='3oKC2'></th></span></q></dt></tr></i><div class="ewyh7er" id='3oKC2'><tfoot id='3oKC2'></tfoot><dl id='3oKC2'><fieldset id='3oKC2'></fieldset></dl></div>
                          1. 主站蜘蛛池模板: 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 泥沙分离_泥沙分离设备_泥砂分离机_洛阳隆中重工机械有限公司 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 微波萃取合成仪-电热消解器价格-北京安合美诚科学仪器有限公司 | 全球化工设备网—化工设备,化工机械,制药设备,环保设备的专业网络市场。 | 无锡不干胶标签,卷筒标签,无锡瑞彩包装材料有限公司 | 深圳品牌设计公司-LOGO设计公司-VI设计公司-未壳创意 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 大学食堂装修设计_公司餐厅效果图_工厂食堂改造_迈普装饰 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 南京泽朗生物科技有限公司| 网站优化公司_SEO优化_北京关键词百度快速排名-智恒博网络 | 深圳APP开发_手机软件APP定制外包_小程序开发公司-来科信 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 开锐教育-学历提升-职称评定-职业资格培训-积分入户 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 苏州防水公司_厂房屋面外墙防水_地下室卫生间防水堵漏-苏州伊诺尔防水工程有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 挖掘机挖斗和铲斗生产厂家选择徐州崛起机械制造有限公司 | 广西正涛环保工程有限公司【官网】 | 贴板式电磁阀-不锈钢-气动上展式放料阀-上海弗雷西阀门有限公司 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 等离子表面处理机-等离子表面活化机-真空等离子清洗机-深圳市东信高科自动化设备有限公司 | 深圳3D打印服务-3D打印加工-手板模型加工厂-悟空打印坊 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 安徽华耐泵阀有限公司-官方网站| MES系统-WMS系统-MES定制开发-制造执行MES解决方案-罗浮云计算 | 北京公司注册_代理记账_代办商标注册工商执照-企力宝 | 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | 香蕉筛|直线|等厚|弧形|振动筛|香蕉筛厂家-洛阳隆中重工 | 淘剧影院_海量最新电视剧,免费高清电影随心观看 | 福尔卡(北京)新型材料技术股份有限公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 脱硫搅拌器厂家-淄博友胜不锈钢搅拌器厂家|