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

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

  • <small id='HNHgq'></small><noframes id='HNHgq'>

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

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

      1. 如何使用相同的參數驗證對同一模擬方法的調用

        How to verify invocations of the same mock method with the same argument that changes state between invocations in mockito?(如何使用相同的參數驗證對同一模擬方法的調用,該參數在模擬中的調用之間改變狀態?)
      2. <small id='y3H7j'></small><noframes id='y3H7j'>

            <bdo id='y3H7j'></bdo><ul id='y3H7j'></ul>
              <tbody id='y3H7j'></tbody>

            • <tfoot id='y3H7j'></tfoot>
              1. <legend id='y3H7j'><style id='y3H7j'><dir id='y3H7j'><q id='y3H7j'></q></dir></style></legend>

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

                1. 本文介紹了如何使用相同的參數驗證對同一模擬方法的調用,該參數在模擬中的調用之間改變狀態?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有以下代碼要進行單元測試:

                  I have the following code to be unit tested:

                  public void foo() {
                      Entity entity = //...
                      persistence.save(entity);
                      entity.setDate(new Date());
                      persistence.save(entity);
                  }
                  

                  我想驗證在第一次調用 persistence.save entity.getDate() 時返回 null.

                  I would like to verify that on the first invocation of persistence.save entity.getDate() returns null.

                  因此我無法使用 Mockito.verify(/*...*/) 因為當時方法 foo 已經完成并且 entity.setDate(Date) 被調用.

                  Therefore I'm unable to use Mockito.verify(/*...*/) because at that time the method foo already completed and entity.setDate(Date) was called.

                  所以我認為我需要在調用發生時對調用進行驗證.如何使用 Mockito 做到這一點?

                  So I think I need to do verifications of invocations already at the time the invocations happen. How do I do this using Mockito?

                  推薦答案

                  我創建了以下 Answer 實現:

                  I created the following Answer implementation:

                  public class CapturingAnswer<T, R> implements Answer<T> {
                  
                      private final Function<InvocationOnMock, R> capturingFunction;
                  
                      private final List<R> capturedValues = new ArrayList<R>();
                  
                      public CapturingAnswer(final Function<InvocationOnMock, R> capturingFunction) {
                          super();
                          this.capturingFunction = capturingFunction;
                      }
                  
                      @Override
                      public T answer(final InvocationOnMock invocation) throws Throwable {
                          capturedValues.add(capturingFunction.apply(invocation));
                          return null;
                      }
                  
                      public List<R> getCapturedValues() {
                          return Collections.unmodifiableList(capturedValues);
                      }
                  
                  }
                  

                  此答案捕獲正在執行的調用的屬性.capturedValues 然后可以用于簡單的斷言.該實現使用 Java 8 API.如果這不可用,則需要使用能夠將 InvocationOnMock 轉換為捕獲值的接口.測試用例中的用法是這樣的:

                  This answer captures properties of the invocations being made. The capturedValues can then be used for simple assertions. The implementation uses Java 8 API. If that is not available one would need to use an interface that is able to convert the InvocationOnMock to the captured value. The usage in the testcase is like this:

                  @Test
                  public void testSomething() {
                      CapturingAnswer<Void,Date> captureDates = new CapturingAnswer<>(this::getEntityDate)
                      Mockito.doAnswer(captureDates).when(persistence).save(Mockito.any(Entity.class));
                  
                      service.foo();
                  
                      Assert.assertNull(captureDates.getCapturedValues().get(0));
                  }
                  
                  private Date getEntityDate(InvocationOnMock invocation) {
                      Entity entity = (Entity)invocation.getArguments()[0];
                      return entity.getDate();
                  }
                  

                  使用 Mockitos ArgumentCaptor 無法實現由呈現的 Answer 實現完成的捕獲,因為這僅在調用被測方法后使用.

                  The capturing that is done by the presented Answer implementation can't be achieved with Mockitos ArgumentCaptor because this is only used after the invocation of the method under test.

                  這篇關于如何使用相同的參數驗證對同一模擬方法的調用,該參數在模擬中的調用之間改變狀態?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

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

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

                            <legend id='pFkpM'><style id='pFkpM'><dir id='pFkpM'><q id='pFkpM'></q></dir></style></legend>
                            主站蜘蛛池模板: 羞羞的视频在线看 | a级毛片免费高清视频 | 在线观看亚洲精品 | 一区二区三区四区不卡 | 中文字幕亚洲无线 | 亚洲欧美国产一区二区三区 | 狠狠综合久久av一区二区小说 | 一级毛片免费视频观看 | 国产欧美日韩在线观看 | 国产精品免费观看 | 久久国产精品99久久久久久丝袜 | 日韩在线视频一区 | 国产精品久久久久久久久久妞妞 | 涩在线 | 老司机深夜福利网站 | 成人在线视频网 | 亚洲精品中文字幕中文字幕 | 国产精品区二区三区日本 | 亚洲人成人一区二区在线观看 | 午夜爱爱毛片xxxx视频免费看 | 91精品国产91久久久久久 | 国产成人麻豆免费观看 | 亚洲福利一区二区 | 欧美色视频免费 | 中文在线一区二区 | 成人免费看 | 欧美视频第二页 | 日韩在线一区二区三区 | 国产精品九九视频 | 久久精品国产99国产精品 | 精品国产高清一区二区三区 | 亚洲国产成人在线 | 久久久精选 | 成人国产精品入口免费视频 | 久久精品欧美一区二区三区不卡 | 日本aa毛片a级毛片免费观看 | 韩日一区二区 | 欧美一级在线免费 | 911精品美国片911久久久 | 在线免费观看日本 | 成人妇女免费播放久久久 |