久久久久久久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. 如何使用相同的參數(shù)驗(yàn)證對(duì)同一模擬方法的調(diào)用

        How to verify invocations of the same mock method with the same argument that changes state between invocations in mockito?(如何使用相同的參數(shù)驗(yàn)證對(duì)同一模擬方法的調(diào)用,該參數(shù)在模擬中的調(diào)用之間改變狀態(tài)?)
      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. 本文介紹了如何使用相同的參數(shù)驗(yàn)證對(duì)同一模擬方法的調(diào)用,該參數(shù)在模擬中的調(diào)用之間改變狀態(tài)?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有以下代碼要進(jìn)行單元測(cè)試:

                  I have the following code to be unit tested:

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

                  我想驗(yàn)證在第一次調(diào)用 persistence.save entity.getDate() 時(shí)返回 null.

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

                  因此我無法使用 Mockito.verify(/*...*/) 因?yàn)楫?dāng)時(shí)方法 foo 已經(jīng)完成并且 entity.setDate(Date) 被調(diào)用.

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

                  所以我認(rèn)為我需要在調(diào)用發(fā)生時(shí)對(duì)調(diào)用進(jìn)行驗(yàn)證.如何使用 Mockito 做到這一點(diǎn)?

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

                  推薦答案

                  我創(chuàng)建了以下 Answer 實(shí)現(xiàn):

                  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);
                      }
                  
                  }
                  

                  此答案捕獲正在執(zhí)行的調(diào)用的屬性.capturedValues 然后可以用于簡單的斷言.該實(shí)現(xiàn)使用 Java 8 API.如果這不可用,則需要使用能夠?qū)?InvocationOnMock 轉(zhuǎn)換為捕獲值的接口.測(cè)試用例中的用法是這樣的:

                  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 無法實(shí)現(xiàn)由呈現(xiàn)的 Answer 實(shí)現(xiàn)完成的捕獲,因?yàn)檫@僅在調(diào)用被測(cè)方法后使用.

                  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.

                  這篇關(guān)于如何使用相同的參數(shù)驗(yàn)證對(duì)同一模擬方法的調(diào)用,該參數(shù)在模擬中的調(diào)用之間改變狀態(tài)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 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>
                            主站蜘蛛池模板: 国产一区2区 | 中文字幕一区二区三区四区五区 | 中文字幕av在线 | 欧美午夜精品理论片a级按摩 | 久久午夜视频 | 久热久 | 午夜欧美| 亚洲日本乱码在线观看 | 一区二区三区四区视频 | 人人干人人超 | 国产传媒视频在线观看 | 午夜专区 | 久久精品视频一区二区三区 | 精品欧美一区二区在线观看欧美熟 | 午夜成人在线视频 | 人人干人人舔 | 日韩在线视频播放 | 在线观看av中文字幕 | 做a视频 | 久久久久久久久久久久久久久久久久久久 | 夜夜爽99久久国产综合精品女不卡 | 亚洲精品久久久一区二区三区 | 国产一级一级毛片 | 久久精品国产一区二区三区不卡 | 九九99九九精彩46 | 精品亚洲一区二区三区四区五区高 | 噜噜噜噜狠狠狠7777视频 | 日日草夜夜草 | 亚洲精品乱码久久久久久久久久 | 久久精品国产免费 | 亚洲日韩中文字幕 | 欧洲精品久久久久毛片完整版 | 一区二区影院 | 亚洲欧美在线视频 | 美女一区 | 国产美女自拍视频 | 国产一区二区在线视频 | 欧美一区免费 | 亚洲福利网站 | 亚洲视频免费一区 | 91国语清晰打电话对白 |