pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <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="xxzzvnx" 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="vndnx7r" 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)行單元測試:

                  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)換為捕獲值的接口.測試用例中的用法是這樣的:

                  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)用被測方法后使用.

                  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?(如何檢測 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="uu0auac" 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>
                            主站蜘蛛池模板: 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 雪花制冰机(实验室雪花制冰机)百科| 珠宝展柜-玻璃精品展柜-首饰珠宝展示柜定制-鸿钛展柜厂家 | 冷水机-工业冷水机-冷水机组-欧科隆品牌保障 | 北京康百特科技有限公司-分子蒸馏-短程分子蒸馏设备-实验室分子蒸馏设备 | 钢板仓,大型钢板仓,钢板库,大型钢板库,粉煤灰钢板仓,螺旋钢板仓,螺旋卷板仓,骨料钢板仓 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 液晶拼接屏厂家_拼接屏品牌_拼接屏价格_监控大屏—北京维康 | 禹城彩钢厂_钢结构板房_彩钢复合板-禹城泰瑞彩钢复合板加工厂 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 天津散热器_天津暖气片_天津安尼威尔散热器制造有限公司 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | 耐磨焊丝,堆焊焊丝,耐磨药芯焊丝,碳化钨焊丝-北京耐默公司 | 干法制粒机_智能干法制粒机_张家港市开创机械制造有限公司 | 整合营销推广|营销网络推广公司|石家庄网站优化推广公司|智营销 好物生环保网、环保论坛 - 环保人的学习交流平台 | 西装定制/做厂家/公司_西装订做/制价格/费用-北京圣达信西装 | 超声骨密度仪-动脉硬化检测仪器-人体成分分析仪厂家/品牌/价格_南京科力悦 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 合金ICP光谱仪(磁性材料,工业废水)-百科 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 火锅底料批发-串串香技术培训[川禾川调官网] | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 重庆LED显示屏_显示屏安装公司_重庆LED显示屏批发-彩光科技公司 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 紫外可见光分光度计-紫外分光度计-分光光度仪-屹谱仪器制造(上海)有限公司 | 凝胶成像系统(wb成像系统)百科-上海嘉鹏 | 立式矫直机_卧式矫直机-无锡金矫机械制造有限公司 | 万博士范文网-您身边的范文参考网站Vanbs.com | 武汉画册印刷厂家-企业画册印刷-画册设计印刷制作-宣传画册印刷公司 - 武汉泽雅印刷厂 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 |