久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      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="qwawimqqmiuu" 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="qwawimqqmiuu" 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?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  假設我有實現它的接口和實現類,我想為此編寫單元測試.我應該測試什么接口或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;
                      }
                  }
                  

                  所以,我有實現它的 HelloInterface 和 HelloInterfaceImpl.什么是被測單元接口或 Impl?

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

                  我覺得應該是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);
                  
                      }
                   }
                  

                  主線實際上是我注釋掉的.

                  The main line is actually one that I commented out.

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

                  方法 setTarget() 不是我的公共接口的一部分,所以我不想不小心 調用它.如果我真的想調用它,我應該花點時間考慮一下.例如,它幫助我發現我真正想做的是依賴注入.它為我打開了整個世界的新機遇.我可以使用一些現有的依賴注入機制(例如 Spring 的),我可以自己模擬它,就像我在代碼中實際所做的那樣,或者采用完全不同的方法.仔細看,準備 PrintSream 沒那么容易,也許我應該改用 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?

                  編輯:我認為我應該始終關注界面.從我的角度來看, setTarget() 也不是 impl 類的合同"的一部分,它為依賴注入服務.我認為從測試的角度來看,任何 Impl 類的公共方法都應該被認為是私有的.但這并不意味著我忽略了實現細節.

                  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.

                  另請參閱是否應該對私有/受保護方法進行單元測試?

                  EDIT-2 在多個實現多個接口的情況下,我會測試所有的實現,但是當我在 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.

                  推薦答案

                  實現是需要測試的單元.這當然是您要實例化的內容以及包含程序/業務邏輯的內容.

                  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.

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

                  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.

                  這篇關于被測單元:Impl 還是 Interface?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

                        <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="qwawimqqmiuu" id='LijAr'><tfoot id='LijAr'></tfoot><dl id='LijAr'><fieldset id='LijAr'></fieldset></dl></div>
                              <tbody id='LijAr'></tbody>

                          • 主站蜘蛛池模板: 四虎影视在线 | 天堂一区二区三区四区 | 午夜大片 | a中文在线视频 | 亚洲另类视频 | 国产精品二区三区 | 精品一区二区三区在线观看 | 久久久久久国产精品久久 | 中文字幕一区在线观看视频 | 在线观看中文字幕视频 | 亚洲一区二区三区四区五区午夜 | 日本免费一区二区三区 | 亚洲欧美日韩在线不卡 | 黄色一级大片在线免费看产 | 日韩国产高清在线观看 | 国产精品99久久免费观看 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 一级免费毛片 | 亚洲在线免费 | 久久久久久毛片免费观看 | 欧美日韩在线一区二区 | 久久爱黑人激情av摘花 | 亚洲午夜精品视频 | 黄色91在线| 久久视频免费看 | 91玖玖| 91极品欧美视频 | 国产在线一区二区三区 | 午夜电影网站 | 中文字幕在线不卡播放 | 国产精品1区2区 | 久久网亚洲 | 一区二区精品 | 亚洲成人综合在线 | 亚洲一二三区免费 | 亚洲福利视频一区二区 | 国产精品久久久乱弄 | 亚洲国产成人精品女人久久久 | 欧美黄在线观看 | 免费看一区二区三区 | av在线一区二区三区 |