久久久久久久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久久精品国产毛片 | 天天成人综合网 | 国产精品久久久爽爽爽麻豆色哟哟 | 日韩欧美日韩在线 | 午夜视频一区二区 | 欧美日韩国产一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 91久久久久久久久久久久久 | 国产精品一区二区在线 | 欧美日韩亚洲一区 | 一区视频在线免费观看 | 中文字幕乱码视频32 | 国产精品免费一区二区三区 | a免费视频| 精品国产欧美一区二区三区成人 | 成人深夜小视频 | 久久狼人天堂 | 91成人影院 | av网站免费看 | h视频在线免费观看 | 特级黄一级播放 | 国产成人精品综合 | 日韩成人| 在线观看免费福利 | 欧洲一区二区三区 | 天天想天天干 | 亚洲综合免费 | 成人国产综合 | 色吧综合网 | 国产伊人精品 | 欧美一级免费看 | 久久久成 | 中文字幕第十一页 | 国产激情在线 | 亚洲视频在线观看 | 91人人看| 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 中文字幕在线一区二区三区 | 日日日干干干 | 亚洲网站在线观看 | av成人在线观看 |