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

      <legend id='U99aW'><style id='U99aW'><dir id='U99aW'><q id='U99aW'></q></dir></style></legend>

      <small id='U99aW'></small><noframes id='U99aW'>

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

      如何處理“任何其他價值"?與 Mockito?

      How to handle quot;any other valuequot; with Mockito?(如何處理“任何其他價值?與 Mockito?)
      <tfoot id='zJWk1'></tfoot>
    2. <legend id='zJWk1'><style id='zJWk1'><dir id='zJWk1'><q id='zJWk1'></q></dir></style></legend>

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

            <tbody id='zJWk1'></tbody>
                <bdo id='zJWk1'></bdo><ul id='zJWk1'></ul>
                <i id='zJWk1'><tr id='zJWk1'><dt id='zJWk1'><q id='zJWk1'><span id='zJWk1'><b id='zJWk1'><form id='zJWk1'><ins id='zJWk1'></ins><ul id='zJWk1'></ul><sub id='zJWk1'></sub></form><legend id='zJWk1'></legend><bdo id='zJWk1'><pre id='zJWk1'><center id='zJWk1'></center></pre></bdo></b><th id='zJWk1'></th></span></q></dt></tr></i><div class="eeigwk2" id='zJWk1'><tfoot id='zJWk1'></tfoot><dl id='zJWk1'><fieldset id='zJWk1'></fieldset></dl></div>
                本文介紹了如何處理“任何其他價值"?與 Mockito?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我有一個接口 Foo 和方法 int Foo.bar(int) 我想用 Mockito 模擬.如果我傳入 1,我希望模擬方法返回 99,但所有其他值都會引發(fā)異常.我可以這樣做嗎?

                I have an interface Foo with method int Foo.bar(int) that I want to mock with Mockito. I want the mocked method to return 99 if I pass in 1, but all other values will throw an exception. Can I do this?

                final Foo foo = mock(Foo.class);
                when(foo.bar(1)).thenReturn(99);
                when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
                

                換句話說,1 會優(yōu)先于 anyInt() 嗎?我不希望它為 1 拋出異常.docs 說對于多個定義,最后一個定義更重要,但我不知道這是否意味著相同的論點.如果在這里適用,我需要先定義通配符 anyInt() 嗎?或者兩者是否有任何關(guān)系,因為其中一個是匹配器,另一個只是一個值?

                In other words, will the 1 take precedence over anyInt()? I wouldn't want it throwing an exception for 1. The docs say that for multiple definitions the last definition is more important, but I couldn't tell if that meant for identical arguments or not. If it applies here, would I need to define the wildcard anyInt() first? Or do the two even have any relation, as one of them is a matcher and the other is just a value?

                推薦答案

                您有兩個選擇:匹配除一個之外的任何值",以及覆蓋存根.(我想您也可以將 Answer 用于復雜的自定義行為,但對于這種情況來說,這太過分了.)

                You have two options: Matching "any value but one", and overriding stubbing. (I suppose you could also use an Answer for complex custom behavior, but that's overkill for situations like this one.)

                Mockito 的 AdditionalMatchers 類提供了許多有用的匹配器,包括操作符,例如 不是.這將允許您為除特定值(或表達式)之外的所有值設(shè)置行為.

                Mockito's AdditionalMatchers class offers a number of useful matchers, including operators such as not. This would allow you to set behavior for all values except for a specific value (or expression).

                when(foo.bar(1)).thenReturn(99);
                when(foo.bar(not(eq(1)))).thenThrow(new IllegalArgumentException());
                

                請注意,運算符必須與匹配器而不是值一起使用,由于 Mockito 的 Matchers.eq 作為顯式 equals 匹配器"https://stackoverflow.com/a/22822514/1426891">參數(shù)匹配器堆棧:

                Be careful to note that operators must be used with matchers instead of values, possibly requiring Matchers.eq as an explicit equals matcher, due to Mockito's argument matcher stack:

                /* BAD */  when(foo.bar(not(  1  ))).thenThrow(new IllegalArgumentException());
                /* GOOD */ when(foo.bar(not(eq(1)))).thenThrow(new IllegalArgumentException());
                

                覆蓋存根

                對于存根,最后定義的匹配鏈獲勝.這允許您在 @Before 方法中設(shè)置一般測試夾具行為,并根據(jù)需要在單個測試用例中覆蓋它,但也意味著在您的存根調(diào)用中順序很重要.

                Overriding stubbing

                For stubbing, the last-defined matching chain wins. This allows you to set up general test fixture behavior in a @Before method and override it in individual test cases if you wish, but also implies that order matters in your stubbing calls.

                when(foo.baz(anyInt())).thenReturn("A", "B");  /* or .thenReturn("A").thenReturn("B"); */
                when(foo.baz(9)).thenReturn("X", "Y");
                
                foo.baz(6); /* "A" because anyInt() is the last-defined chain */
                foo.baz(7); /* "B" as the next return value of the first chain */
                foo.baz(8); /* "B" as Mockito repeats the final chain action forever */
                
                foo.baz(9); /* "X" because the second chain matches for the value 9 */
                foo.baz(9); /* "Y" forever because the second chain action still matches */
                

                因此,您永遠不會看到問題中列出的順序中的兩個存根,因為如果一般匹配緊跟特定匹配,則永遠不會使用特定匹配(也可能被刪除).

                Consequently, you should never see the two stubs in the order listed in the question, because if a general match immediately follows a specific match then the specific match will never be used (and may as well be deleted).

                請注意,在覆蓋間諜或危險的存根行為時,您有時需要將語法更改為 doAnswer.Mockito 知道不計算對 when 的調(diào)用以進行驗證或沿 thenVerb 鏈前進,但異常仍可能導致您的測試失敗.

                Beware that you'll sometimes need to change syntax to doAnswer when overriding spies or dangerous stubbed behavior. Mockito knows not to count calls to when for verification or for advancing along thenVerb chains, but exceptions could still cause your test to fail.

                /* BAD: the call to foo.bar(1) will throw before Mockito has a chance to stub it! */
                when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
                when(foo.bar(1)).thenReturn(99);
                
                /* GOOD: Mockito has a chance to deactivate behavior during stubbing. */
                when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
                doReturn(99).when(foo).bar(1);
                

                這篇關(guān)于如何處理“任何其他價值"?與 Mockito?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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)建一個隨機打亂數(shù)字的 int 數(shù)組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)

                <small id='2489G'></small><noframes id='2489G'>

                  <bdo id='2489G'></bdo><ul id='2489G'></ul>
                    <tbody id='2489G'></tbody>

                • <legend id='2489G'><style id='2489G'><dir id='2489G'><q id='2489G'></q></dir></style></legend>
                • <tfoot id='2489G'></tfoot>

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

                        • 主站蜘蛛池模板: 武汉高低温试验箱_恒温恒湿试验箱厂家-武汉蓝锐环境科技有限公司 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 一体式钢筋扫描仪-楼板测厚仪-裂缝检测仪-泰仕特(北京) | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 翰香原枣子坊加盟费多少钱-正宗枣核糕配方培训利润高飘香 | 两头忙,井下装载机,伸缩臂装载机,30装载机/铲车,50装载机/铲车厂家_价格-莱州巨浪机械有限公司 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 幂简集成 - 品种超全的API接口平台, 一站搜索、试用、集成国内外API接口 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | 臭氧灭菌箱-油桶加热箱-原料桶加热融化烘箱-南京腾阳干燥设备厂 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 奥运星-汽车性能网评-提供个性化汽车资讯 | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 集装箱展厅-住人集装箱住宿|建筑|房屋|集装箱售楼处-山东锐嘉科技工程有限公司 | 润东方环保空调,冷风机,厂房车间降温设备-20年深圳环保空调生产厂家 | 贵州自考_贵州自学考试网| 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 国际线缆连接网 - 连接器_线缆线束加工行业门户网站 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 | 手持式3d激光扫描仪-便携式三维立体扫描仪-北京福禄克斯 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 网站建设-网站制作-网站设计-网站开发定制公司-网站SEO优化推广-咏熠软件 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 |