久久久久久久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>
                            主站蜘蛛池模板: 亚洲国产精品99久久久久久久久 | h视频在线观看免费 | 国产一级免费视频 | 日韩视频在线观看中文字幕 | 亚洲成人精品免费 | 特一级毛片 | 久久一区二区视频 | 欧美激情精品久久久久久 | 欧美成人一区二区三区 | 国产精品自产av一区二区三区 | 亚洲精品国产成人 | 成人二区| 精品国产99 | 国产免费拔擦拔擦8x高清 | 国产精品a久久久久 | 亚洲视频免费观看 | 欧美一区二区三区视频 | 先锋资源亚洲 | 日韩精品一二三 | 手机看片1 | 一级做a爰片久久毛片 | 国产精品不卡一区二区三区 | 欧美日韩三级在线观看 | a在线免费观看视频 | 日韩国产欧美在线观看 | 色综合久久天天综合网 | 国产日韩精品一区二区 | 午夜视频网站 | 亚洲一区国产精品 | 日韩字幕 | 91视频免费黄 | 在线播放一区二区三区 | 91aiai| 噜久寡妇噜噜久久寡妇 | 久久国产精品视频 | 久久精品国产一区二区电影 | 色婷婷亚洲一区二区三区 | 91久久综合亚洲鲁鲁五月天 | 亚洲成人免费在线 | 欧美日韩精品在线一区 | 日韩欧美三区 |