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

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

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

      • <bdo id='HOo7H'></bdo><ul id='HOo7H'></ul>
      <tfoot id='HOo7H'></tfoot>
      1. 用 Mockito 模擬重載的方法

        Mocking Overloaded Methoods With Mockito(用 Mockito 模擬重載的方法)

          1. <small id='AL1zV'></small><noframes id='AL1zV'>

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

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

                  本文介紹了用 Mockito 模擬重載的方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在測試一些依賴于 RestTemplate 類中的 getForObject() 方法的方法.

                  I am testing some methods that rely on the getForObject() method in the RestTemplate class.

                  getForObject() 方法重載了簽名getForObject(String url, Class responseType, Object... uriVariables)getForObject(字符串 url, 類 responseType, Map

                  The getForObject() method is overloaded with the signaturesgetForObject(String url, Class<T> responseType, Object... uriVariables) and getForObject(String url, Class<T> responseType, Map<String, ?>

                  我需要在其參數中使用 Object... 存根方法以引發異常,但我不能因為 Mockito.any() 還包含 地圖類型.因此,將方法存根為 getForObject(Mockito.anyString(),Mockito.any(), Mockito.any() 將指向觸發編譯錯誤的兩個方法.

                  I need to stub the method with Object... in its arguments to throw an exception but I can not because Mockito.any() also encompasses the Map type. Therefore, stubbing the method as getForObject(Mockito.anyString(),Mockito.any(), Mockito.any() will point to BOTH methods triggering a compilation error.

                  這個問題有什么可能的解決方法嗎?

                  Are there any possible workarounds to this problem?

                  我已經嘗試使用 Mockito.anyObject() 無濟于事

                  I have already tried using Mockito.anyObject() to no avail

                  推薦答案

                  不確定你的問題可能是什么,但在這一點上,我不妨發布一個工作示例.

                  Not sure what your problem might be, but at this point I might as well just post a working example.

                  如前所述,您需要正確指定每個參數的類型,以便 mockito 可以定位到匹配的方法簽名.

                  As mentioned before you need to properly specify the type of each parameter, so that mockito can locate the matching method signature.

                  有關處理舊 mockito 版本使用的可變參數的語法,請查看 這個答案.

                  For the syntax to handle varargs used by older mockito versions, check this answer.

                  import static org.mockito.ArgumentMatchers.any;
                  ...
                  
                  @RunWith(MockitoJUnitRunner.class)
                  public class MockitoTest {
                  
                      @Test
                      public void test() throws Exception {
                  
                          RestTemplate api = Mockito.mock(RestTemplate.class);
                  
                          Object obj1 = new Object();
                          Object obj2 = new Object();
                          Object obj3 = new Object();
                  
                          Mockito.when(api.getForObject(any(String.class),any(Class.class), ArgumentMatchers.<Object>any())).thenReturn(obj1);
                          Mockito.when(api.getForObject(any(String.class),any(Class.class), any(Map.class))).thenReturn(obj2);
                          Mockito.when(api.getForObject(any(URI.class),any(Class.class))).thenReturn(obj3);
                  
                          Assert.assertEquals(obj1, api.getForObject("", String.class));
                          Assert.assertEquals(obj1, api.getForObject("", String.class, obj1));
                          Assert.assertEquals(obj1, api.getForObject("", String.class, obj1, obj2));
                          Assert.assertEquals(obj1, api.getForObject("", String.class, obj1, obj2, obj3));
                          Assert.assertEquals(obj1, api.getForObject("", String.class, new Object[] {obj1,obj2,obj3}));
                  
                          Assert.assertEquals(obj2, api.getForObject("", String.class, new HashMap()));
                  
                          Assert.assertEquals(obj3, api.getForObject(new URI(""), String.class));
                      }
                  }
                  

                  對于您的用例,只需將 thenReturn 替換為 thenThrow.

                  For your usecase just replace the thenReturn with thenThrow.

                  這篇關于用 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='fTNrC'></bdo><ul id='fTNrC'></ul>

                        <tbody id='fTNrC'></tbody>
                      <legend id='fTNrC'><style id='fTNrC'><dir id='fTNrC'><q id='fTNrC'></q></dir></style></legend><tfoot id='fTNrC'></tfoot>
                    • <small id='fTNrC'></small><noframes id='fTNrC'>

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

                            主站蜘蛛池模板: 黄色av观看 | 国产美女在线免费观看 | 中文字幕乱码一区二区三区 | 亚洲图片视频一区 | 日屁网站 | 亚洲一区在线观看视频 | 亚洲a一区| 欧美久久一区 | 欧美性a视频 | 日日摸天天添天天添破 | 免费成人毛片 | 在线观看国产三级 | va精品 | 午夜电影日韩 | 国产免费一区二区 | 久久精品久久久久久 | 粉嫩高清一区二区三区 | 亚洲成人一区 | 日本爱爱| 午夜天堂精品久久久久 | 国产精品18hdxxxⅹ在线 | 天天曰夜夜操 | 成人在线视 | 欧美一区二区三区精品免费 | 亚洲乱码国产乱码精品精98午夜 | 中文字幕在线一区二区三区 | 亚洲国产一区二区视频 | 久久精品久久综合 | 日韩午夜影院 | 国产精品美女久久久免费 | 完全免费在线视频 | 人人精品 | 野狼在线社区2017入口 | 日韩视频在线免费观看 | 久久精品16 | 这里有精品 | 国产偷久久一级精品60部 | 日韩在线欧美 | 日本免费一区二区三区 | 亚洲人在线| 亚洲三区在线 |