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

      1. <tfoot id='uNbNh'></tfoot>

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

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

        被測單元:Impl 還是 Interface?

        Unit under test: Impl or Interface?(被測單元:Impl 還是 Interface?)
          <tbody id='lAADR'></tbody>

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

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

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

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

                  本文介紹了被測單元:Impl 還是 Interface?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  假設(shè)我有實(shí)現(xiàn)它的接口和實(shí)現(xiàn)類,我想為此編寫單元測試.我應(yīng)該測試什么接口或Impl?

                  Suppose I have interface and implementation class that implements it and I want to write unit-test for this. What should I test interface or Impl?

                  這是一個例子:

                  public interface HelloInterface {
                      public void sayHello();
                  }
                  
                  
                  public class HelloInterfaceImpl implements HelloInterface {
                      private PrintStream target = System.out;
                  
                  
                      @Override
                      public void sayHello() {
                          target.print("Hello World");
                  
                      }
                  
                      public void setTarget(PrintStream target){
                          this.target = target;
                      }
                  }
                  

                  所以,我有實(shí)現(xiàn)它的 HelloInterface 和 HelloInterfaceImpl.什么是被測單元接口或 Impl?

                  So, I have HelloInterface and HelloInterfaceImpl that implements it. What is unit-under-test interface or Impl?

                  我覺得應(yīng)該是HelloInterface.考慮下面的 JUnit 測試草圖:

                  I think it should be HelloInterface. Consider following sketch of JUnit test:

                  public class HelloInterfaceTest {
                      private HelloInterface hi;
                  
                      @Before
                      public void setUp() {
                          hi = new HelloInterfaceImpl();
                      }
                  
                      @Test
                      public void testDefaultBehaviourEndsNormally() {
                          hi.sayHello();
                          // no NullPointerException here
                      }
                  
                      @Test
                      public void testCheckHelloWorld() throws Exception {
                          ByteArrayOutputStream out = new ByteArrayOutputStream();
                          PrintStream target = new PrintStream(out);
                          PrivilegedAccessor.setValue(hi, "target", target);
                          //You can use ReflectionTestUtils in place of PrivilegedAccessor
                          //really it is DI 
                          //((HelloInterfaceImpl)hi).setTarget(target);
                          hi.sayHello();
                          String result = out.toString();
                          assertEquals("Hello World", result);
                  
                      }
                   }
                  

                  主線實(shí)際上是我注釋掉的.

                  The main line is actually one that I commented out.

                  ((HelloInterfaceImpl)hi).setTarget(target);

                  方法 setTarget() 不是我的公共接口的一部分,所以我不想不小心 調(diào)用它.如果我真的想調(diào)用它,我應(yīng)該花點(diǎn)時間考慮一下.例如,它幫助我發(fā)現(xiàn)我真正想做的是依賴注入.它為我打開了整個世界的新機(jī)遇.我可以使用一些現(xiàn)有的依賴注入機(jī)制(例如 Spring 的),我可以自己模擬它,就像我在代碼中實(shí)際所做的那樣,或者采用完全不同的方法.仔細(xì)看,準(zhǔn)備 PrintSream 沒那么容易,也許我應(yīng)該改用 mock 對象?

                  Method setTarget() is not part of my public interface, so I don't want to accidentally call it. If I really want to call it, I should take a moment and think about it. It helps me, for example, to discover that what I'm really trying to do is dependency injection. It opens for me the whole world of new opportunities. I can use some existing dependency injection mechanism (Spring's, for example), I can simulate it myself as I actually did in my code or to take totally different approach. Take a closer look, preparation of PrintSream wasn't that easy, maybe I should use mock object instead?

                  編輯:我認(rèn)為我應(yīng)該始終關(guān)注界面.從我的角度來看, setTarget() 也不是 impl 類的合同"的一部分,它為依賴注入服務(wù).我認(rèn)為從測試的角度來看,任何 Impl 類的公共方法都應(yīng)該被認(rèn)為是私有的.但這并不意味著我忽略了實(shí)現(xiàn)細(xì)節(jié).

                  EDIT: I think I should always focus on the interface. From my point of view setTarget() is not part of the "contract" of the impl class neither, it serves sally for dependency injection. I think any public method of Impl class should be considered as private from the testing perspective. It doesn't mean that I ignore the implementation details, though.

                  另請參閱是否應(yīng)該對私有/受保護(hù)方法進(jìn)行單元測試?

                  EDIT-2 在多個實(shí)現(xiàn)多個接口的情況下,我會測試所有的實(shí)現(xiàn),但是當(dāng)我在 setUp() 方法中聲明一個變量時我肯定會使用界面.

                  EDIT-2 In the case of multiple implementationsmultiple interfaces, I would test all of the implementations, but when I declare a variable in my setUp() method I would definitely use interface.

                  推薦答案

                  實(shí)現(xiàn)是需要測試的單元.這當(dāng)然是您要實(shí)例化的內(nèi)容以及包含程序/業(yè)務(wù)邏輯的內(nèi)容.

                  The implementation is the unit that needs to be tested. That is of course what you are instantiating and what contains the program/business logic.

                  如果您有一個關(guān)鍵接口,并且希望確保每個實(shí)現(xiàn)都正確地遵守它,那么您可以編寫一個專注于接口并要求傳入實(shí)例的測試套件(與任何實(shí)現(xiàn)類型無關(guān)).

                  If you had a critical interface and you wanted to make sure every implementation adhered to it properly, then you may write a test suite that focuses on the interface and requires an instance be passed in (agnostic of any implementation type).

                  是的,將 Mockito 用于 PrintStream 可能會更容易,但可能并不總是可以避免像在此特定示例中那樣使用模擬對象.

                  Yes, it would probably be easier to use Mockito for PrintStream, it may not always be possible to avoid using a mock object like you did in this specific example.

                  這篇關(guān)于被測單元:Impl 還是 Interface?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)建一個隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)

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

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

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

                          • 主站蜘蛛池模板: 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 气密性检测仪_气密性检测设备_防水测试仪_密封测试仪-岳信仪器 | 翅片管散热器价格_钢制暖气片报价_钢制板式散热器厂家「河北冀春暖气片有限公司」 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 郑州墨香品牌设计公司|品牌全案VI设计公司 | 单柱拉力机-橡胶冲片机-哑铃裁刀-江都轩宇试验机械厂 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 圆窗水平仪|伊莉莎冈特elesa+ganter | 扬尘监测_扬尘监测系统_带证扬尘监测设备 - 郑州港迪科技有限公司 | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | 成都租车_成都租车公司_成都租车网_众行宝 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 广东佛电电器有限公司|防雷开关|故障电弧断路器|智能量测断路器 广东西屋电气有限公司-广东西屋电气有限公司 | 无痕胶_可移胶_无痕双面胶带_可移无痕胶厂家-东莞凯峰 | 泰安办公家具-泰安派格办公用品有限公司 | 六自由度平台_六自由度运动平台_三自由度摇摆台—南京全控科技 | 无刷电机_直流无刷电机_行星减速机-佛山市藤尺机电设备有限公司 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 陶氏道康宁消泡剂_瓦克消泡剂_蓝星_海明斯德谦_广百进口消泡剂 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 工业制氮机_psa制氮机厂家-宏骁智能装备科技江苏有限公司 | 氧化铁红厂家-淄博宗昂化工 | 缓蚀除垢剂_循环水阻垢剂_反渗透锅炉阻垢剂_有机硫化物-郑州威大水处理材料有限公司 | 蔬菜清洗机_环速洗菜机_异物去除清洗机_蔬菜清洗机_商用洗菜机 - 环速科技有限公司 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | 滚筒烘干机_转筒烘干机_滚筒干燥机_转筒干燥机_回转烘干机_回转干燥机-设备生产厂家 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 不锈钢水箱厂家,不锈钢保温水箱-山东桑特供水设备 | 深圳市八百通智能技术有限公司官方网站| 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 家德利门业,家居安全门,别墅大门 - 安徽家德利门业有限公司 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 |