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

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

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

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

        使用 mockito 模擬使用通配符返回泛型的方法

        mocking a method that return generics with wildcard using mockito(使用 mockito 模擬使用通配符返回泛型的方法)

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

        <tfoot id='BVfxb'></tfoot>

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

              <bdo id='BVfxb'></bdo><ul id='BVfxb'></ul>

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

                    <tbody id='BVfxb'></tbody>
                  本文介紹了使用 mockito 模擬使用通配符返回泛型的方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我使用的是 mockito 1.9.5.我有以下代碼:

                  公共類 ClassA {公共列表getMyInterfaces() {返回空值;}公共靜態無效testMock(){列出<我的界面>接口 = 新的 ArrayList<>();ClassA classAMock = mock(ClassA.class);when(classAMock.getMyInterfaces()).thenReturn(interfaces);}

                  我得到一個編譯錯誤 thenReturn(interfaces) 說:

                  "類型中的thenReturn(List)方法OngoingStubbing<List<capture#1-of ?擴展我的接口>>不適用于論點(列表<MyInterface>)"

                  但是,當我使用 mockito 的 thenAnswer 方法時,我沒有收到錯誤消息.誰能告訴我發生了什么事?為什么我在使用 thenReturn 方法時會收到錯誤消息?當 ClassA 由第三方提供且無法修改時,是否有其他方法可以解決此問題?

                  解決方案

                  編輯:從 Mockito 1.10.x 開始,嵌入在類中的泛型類型現在被 Mockito 用于深度存根.IE.

                  公共接口 A<T 擴展 Observer &可比

                  Mockito 盡力獲取編譯器嵌入的類型信息,但是當應用擦除時,mockito 只能返回 Object 的模擬.

                  <小時>

                  原創:嗯,這更多是泛型的問題,而不是 Mockito 的問題.對于泛型,您應該閱讀 Angelika Langer 在它們上寫的內容.對于當前主題,即通配符,請閱讀此部分.p>

                  但簡而言之,您可以使用 Mockito 的其他語法來幫助您解決當前的情況:

                  doReturn(interfaces).when(classAMock).getMyInterfaces();

                  或者使用 BDD 別名:

                  willReturn(interfaces).given(classAMock).getMyInterfaces();

                  盡管如此,您可以編寫更通用友好的包裝器.這將有助于未來的開發人員使用相同的第 3 方 API.

                  <小時>

                  附帶說明:您不應該模擬您不擁有的類型,這可能會導致許多錯誤和問題.相反,你應該有一些包裝.例如 DAO 和存儲庫就代表了這樣的想法,人們將模擬 DAO 或存儲庫接口,而不是 JDBC/JPA/hibernate 的東西.有很多關于此的博客文章:

                  • http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html
                  • http://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html
                  • https://web.archive.org/web/20140923101818/http://freshbrewedcode.com/derekgreer/2012/04/01/tdd-best-practices-dont-mock-others/
                  • ...

                  I'm using mockito 1.9.5. I have the following code:

                  public class ClassA  {
                  
                  public List<? extends MyInterface> getMyInterfaces() {
                      return null;
                  }
                  
                  public static void testMock() {
                      List<MyInterface> interfaces = new ArrayList<>();
                      ClassA classAMock = mock(ClassA.class);
                      when(classAMock.getMyInterfaces()).thenReturn(interfaces);      
                  }
                  

                  I get a compilation error for the thenReturn(interfaces) saying:

                  "The method thenReturn(List<capture#1-of ? extends MyInterface>) in the type 
                   OngoingStubbing<List<capture#1-of ? extends MyInterface>> is not applicable for the arguments 
                   (List<MyInterface>)"
                  

                  However, when I use the thenAnswer method of mockito, I don't get the error. Can anyone tell me what's going on? Why do I get the error when I use the thenReturn method? Is there any other way to solve this problem when ClassA is provided by a 3rd party and cannot be modified?

                  解決方案

                  EDIT : Starting from Mockito 1.10.x, generics types that are embedded in the class are now used by Mockito for deep stubs. ie.

                  public interface A<T extends Observer & Comparable<? super T>>  {
                    List<? extends B> bList();
                    T observer();
                  }
                  
                  B b = deep_stubbed.bList().iterator().next(); // returns a mock of B ; mockito remebers that A returns a List of B
                  Observer o = deep_stubbed.observer(); // mockito can find that T super type is Observer
                  Comparable<? super T> c = deep_stubbed.observer(); // or that T implements Comparable
                  

                  Mockito tries its best to get type information that the compiler embeds, but when erasure applies, mockito cannot do anything but return a mock of Object.


                  Original : Well that's more of an issue with generics than with Mockito. For generics, you should read what Angelika Langer wrote on them. And for the current topic, i.e. wildcards, read this section.

                  But for short, what you could use is the other syntax of Mockito to help with your current situation :

                  doReturn(interfaces).when(classAMock).getMyInterfaces();
                  

                  Or with the BDD aliases :

                  willReturn(interfaces).given(classAMock).getMyInterfaces();
                  

                  Nevertheless, you could write wrappers that are more generic friendly. That will help future developers working with same 3rd party API.


                  As a side note: you shouldn't mocks type you don't own, it can lead to many errors and issues. Instead you should have some wrapper. DAO and repositories for example represent such idea, one will mock the DAO or repository interface, but not the JDBC / JPA / hibernate stuff. There are many blog posts about that:

                  • http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html
                  • http://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html
                  • https://web.archive.org/web/20140923101818/http://freshbrewedcode.com/derekgreer/2012/04/01/tdd-best-practices-dont-mock-others/
                  • ...

                  這篇關于使用 mockito 模擬使用通配符返回泛型的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='LGZ5b'></bdo><ul id='LGZ5b'></ul>

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

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

                        1. <legend id='LGZ5b'><style id='LGZ5b'><dir id='LGZ5b'><q id='LGZ5b'></q></dir></style></legend>
                          <tfoot id='LGZ5b'></tfoot>
                            <tbody id='LGZ5b'></tbody>
                            主站蜘蛛池模板: 亚洲综合色视频在线观看 | 亚洲免费婷婷 | 81精品国产乱码久久久久久 | 特级毛片www| 国产精品一区一区 | 欧美中文字幕在线观看 | 9久久精品| 午夜国产在线 | 精品欧美一区二区精品久久久 | 成人免费视频网址 | 91久久精品国产91久久性色tv | 在线国产精品一区 | 在线中文字幕第一页 | 97国产爽爽爽久久久 | 中文字幕在线视频精品 | 久久久精品天堂 | 亚洲成人综合社区 | 国产91精品久久久久久久网曝门 | 97人澡人人添人人爽欧美 | 国产美女自拍视频 | 精品久久久av| 成人欧美一区二区三区黑人孕妇 | 9191在线观看| 欧美一区| 成人高清在线视频 | 在线播放亚洲 | 久久久久成人精品 | 久久久久精 | 亚洲精选一区二区 | 国产精品视频在线观看 | 国产免费自拍 | 97影院在线午夜 | 99久久久久国产精品免费 | 国产欧美精品区一区二区三区 | 国产成人a亚洲精品 | 精品国产一区二区三区久久 | 日韩最新网站 | 一区中文字幕 | 在线国产一区二区 | 国产露脸对白88av | 日韩免费在线观看视频 |