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

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

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

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

        在匿名類中測試方法時,如何使用 Powermockito 模擬

        How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?(在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構造?)
          <tbody id='rk5Yf'></tbody>
          <i id='rk5Yf'><tr id='rk5Yf'><dt id='rk5Yf'><q id='rk5Yf'><span id='rk5Yf'><b id='rk5Yf'><form id='rk5Yf'><ins id='rk5Yf'></ins><ul id='rk5Yf'></ul><sub id='rk5Yf'></sub></form><legend id='rk5Yf'></legend><bdo id='rk5Yf'><pre id='rk5Yf'><center id='rk5Yf'></center></pre></bdo></b><th id='rk5Yf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='rk5Yf'><tfoot id='rk5Yf'></tfoot><dl id='rk5Yf'><fieldset id='rk5Yf'></fieldset></dl></div>

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

              <tfoot id='rk5Yf'></tfoot>
            1. <legend id='rk5Yf'><style id='rk5Yf'><dir id='rk5Yf'><q id='rk5Yf'></q></dir></style></legend>
                  <bdo id='rk5Yf'></bdo><ul id='rk5Yf'></ul>

                  本文介紹了在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構造?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想編寫一個 JUnit 測試來驗證下面的代碼是否使用了 BufferedInputStream:

                  I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream:

                  public static final FilterFactory BZIP2_FACTORY = new FilterFactory() {
                      public InputStream makeFilter(InputStream in) {        
                          // a lot of other code removed for clarity 
                          BufferedInputStream buffer = new BufferedInputStream(in);
                          return new CBZip2InputStream(buffer);
                      }
                  };
                  

                  (FilterFactory 是一個接口.)

                  (FilterFactory is an interface.)

                  到目前為止,我的測試如下所示:

                  My test thus far looks like this:

                  @Test
                  public void testBZIP2_FactoryUsesBufferedInputStream() throws Throwable {
                      InputStream in = mock(InputStream.class);
                      BufferedInputStream buffer = mock(BufferedInputStream.class);
                      CBZip2InputStream expected = mock(CBZip2InputStream.class);
                  
                      PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
                      whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
                      whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
                      InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);
                  
                      assertEquals(expected, observed);
                  }
                  

                  對 PowerMockito.spy 的調用引發異常并顯示以下消息:

                  The call to PowerMockito.spy raises an exception with this message:

                  org.mockito.exceptions.base.MockitoException: 
                  Mockito cannot mock this class: class edu.gvsu.cis.kurmasz.io.InputHelper$1
                  Mockito can only mock visible & non-final classes.
                  

                  我應該使用什么來代替 PowerMocktio.spy 來設置對 whenNew 的調用?

                  What should I be using instead of PowerMocktio.spy to set up the calls to whenNew?

                  推薦答案

                  信息很明顯:你不能模擬非可見類和 final 類.簡短回答:為您的匿名類創建一個命名類,然后測試這個類

                  The message is pretty obvious: You can't mock non-visible and final classes. Short answer : Create a named class of your anonymous one, and test this class instead!

                  長答案,讓我們來挖掘一下原因!

                  Long answer, let's dig why !

                  你實例化一個FilterFactory的匿名類,當編譯器看到一個匿名類時,它會創建一個finalpackage visible類.所以匿名類不能通過標準方法模擬,即通過 Mockito.

                  You instantiate an anonymous class of FilterFactory, when the compiler sees an anonymous class, it creates a final and package visible class. So the anonymous class is not mockable through standard mean i.e. through Mockito.

                  好的,現在假設您希望能夠通過 Powermock 模擬這個匿名類.當前編譯器使用以下方案編譯匿名類:

                  OK, now suppose you want to be able to mock this anonymous class through Powermock. Current compilers compile anonymous class with following scheme :

                  Declaring class + $ + <order of declaration starting with 1>
                  

                  模擬匿名類可能但很脆弱(我是認真的)所以假設匿名類是第 11 個被聲明的類,它會顯示為

                  Mocking anonymous class possible but brittle (And I mean it) So supposing the anonymous class is the eleventh to be declared, it will appear as

                  InputHelper$11.class
                  

                  所以你可以準備測試匿名類:

                  So you could potentially prepare for test the anonymous class:

                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest({InputHelper$11.class})
                  public class InputHelperTest {
                      @Test
                      public void anonymous_class_mocking works() throws Throwable {
                          PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
                      }
                  }
                  

                  此代碼將編譯,但最終會在您的 IDE 中報告為錯誤.IDE 可能不知道 InputHelper$11.class.不使用編譯類的IntelliJ檢查代碼報告如此.

                  This code will compile, BUT will eventually be reported as an error with your IDE. The IDE probably doesn't know about InputHelper$11.class. IntelliJ who doesn't use compiled class to check the code report so.

                  匿名類命名實際上取決于聲明的順序這一事實也是一個問題,當有人之前添加另一個匿名類時,編號可能會改變.匿名類是為了保持匿名,如果編譯器決定有一天使用字母甚至隨機標識符怎么辦!

                  Also the fact that the anonymous class naming actually depends on the order of the declaration is a problem, when someone adds another anonymous class before, the numbering could change. Anonymous classes are made to stay anonymous, what if the compiler guys decide one day to use letters or even random identifiers!

                  所以通過 Powermock 模擬匿名類是可能的,但很脆弱,千萬不要在實際項目中這樣做!

                  編輯說明: Eclipse 編譯器有不同的編號方案,它總是使用 3 位數字:

                  EDITED NOTE : The Eclipse compiler has a different numbering scheme, it always uses a 3 digit number :

                  Declaring class + $ + <pad with 0> + <order of declaration starting with 1>
                  

                  另外,我認為 JLS 并沒有明確規定編譯器應該如何命名匿名類.

                  Also I don't think the JLS clearly specify how the compilers should name anonymous classes.

                  PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
                  whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
                  whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
                  InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);
                  

                  PowerMockito.spy 返回 spy,它不會改變 InputHelper.BZIP2_FACTORY 的值.所以你需要通過反射來實際設置這個字段.您可以使用 Powermock 提供的 Whitebox 實用程序.

                  PowerMockito.spy returns the spy, it doesn't change the value of InputHelper.BZIP2_FACTORY. So you would need to actually set via reflection this field. You can use the Whiteboxutility that Powermock provide.

                  只用模擬來測試匿名過濾器使用 BufferedInputStream 太麻煩了.

                  Too much trouble to just test with mocks that the anonymous filter uses a BufferedInputStream.

                  我寧愿寫如下代碼:

                  一個將使用命名類的輸入助手,我不使用接口名稱向用戶說明此過濾器的意圖是什么!

                  An input helper that will use the named class, I don't use the interface name to make clear to the user what is the intent of this filter!

                  public class InputHelper {
                      public static final BufferedBZIP2FilterFactory BZIP2_FACTORY = new BufferedBZIP2FilterFactory();
                  }
                  

                  現在是過濾器本身:

                  public class BufferedBZIP2FilterFactory {
                      public InputStream makeFilter(InputStream in) {
                          BufferedInputStream buffer = new BufferedInputStream(in);
                          return new CBZip2InputStream(buffer);
                      }
                  }
                  

                  現在你可以編寫這樣的測試了:

                  Now you can write a test like this :

                  @RunWith(PowerMockRunner.class)
                  public class BufferedBZIP2FilterFactoryTest {
                  
                      @Test
                      @PrepareForTest({BufferedBZIP2FilterFactory.class})
                      public void wraps_InputStream_in_BufferedInputStream() throws Exception {
                          whenNew(CBZip2InputStream.class).withArguments(isA(BufferedInputStream.class))
                                  .thenReturn(Mockito.mock(CBZip2InputStream.class));
                  
                          new BufferedBZIP2FilterFactory().makeFilter(anInputStream());
                  
                          verifyNew(CBZip2InputStream.class).withArguments(isA(BufferedInputStream.class));
                      }
                  
                      private ByteArrayInputStream anInputStream() {
                          return new ByteArrayInputStream(new byte[10]);
                      }
                  }
                  

                  但如果你強制 CBZip2InputStream 只接受 BufferedInputStream,最終可以避免這個測試場景的 powermock 東西.通常使用 Powermock 意味著設計有問題.在我看來,Powermock 非常適合遺留軟件,但在設計新代碼時會使開發人員失明;由于他們錯過了 OOP 的優點,我什至會說他們正在設計遺留代碼.

                  But could eventually avoid powermock stuff for this test scenario if you force the CBZip2InputStream to only accept BufferedInputStream. Usually using Powermock means something is wrong with the design. In my opinion Powermock is great for legacy softwares, but can blind developers when designing new code; as they are missing the point of OOP's good part, I would even say they are designing legacy code.

                  希望有幫助!

                  這篇關于在匿名類中測試方法時,如何使用 Powermockito 模擬新對象的構造?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='Mhh02'></tfoot><legend id='Mhh02'><style id='Mhh02'><dir id='Mhh02'><q id='Mhh02'></q></dir></style></legend>

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

                          <tbody id='Mhh02'></tbody>

                          <i id='Mhh02'><tr id='Mhh02'><dt id='Mhh02'><q id='Mhh02'><span id='Mhh02'><b id='Mhh02'><form id='Mhh02'><ins id='Mhh02'></ins><ul id='Mhh02'></ul><sub id='Mhh02'></sub></form><legend id='Mhh02'></legend><bdo id='Mhh02'><pre id='Mhh02'><center id='Mhh02'></center></pre></bdo></b><th id='Mhh02'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Mhh02'><tfoot id='Mhh02'></tfoot><dl id='Mhh02'><fieldset id='Mhh02'></fieldset></dl></div>
                          • <bdo id='Mhh02'></bdo><ul id='Mhh02'></ul>
                            主站蜘蛛池模板: 午夜精品在线观看 | 天天久久 | 999精品视频在线观看 | 日本韩国电影免费观看 | 日本一区二区高清不卡 | 欧美亚洲国产一区二区三区 | 九九久久精品视频 | 国产精品久久久av | 久久久久久久久久久国产 | 日韩午夜精品 | 国产精品久久久久久久久久三级 | 日本精品视频在线观看 | 日本精品视频 | 成人精品在线观看 | 亚洲精品乱码8久久久久久日本 | 国产精品久久久久久久久久久久久 | 黄色毛片在线看 | 欧美阿v | 精品网 | av一级毛片 | 超碰在线国产 | 欧美在线国产精品 | 青青草精品视频 | av片在线免费看 | 欧美亚洲高清 | 九色在线观看 | 久草新在线 | 五月婷婷视频 | 性高朝久久久久久久3小时 av一区二区三区四区 | 日韩精品一 | 亚洲成a| 久久99精品久久久久 | 国产高潮好爽受不了了夜色 | 久久尤物免费一区二区三区 | 亚洲永久 | 亚洲精品电影网在线观看 | 国产伦精品一区二区三区精品视频 | 一二三区av | 四虎影院免费在线播放 | 欧美日韩三级在线观看 | 国产成人99久久亚洲综合精品 |