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

<tfoot id='teyGe'></tfoot>
    1. <legend id='teyGe'><style id='teyGe'><dir id='teyGe'><q id='teyGe'></q></dir></style></legend>
        <bdo id='teyGe'></bdo><ul id='teyGe'></ul>

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

    2. <i id='teyGe'><tr id='teyGe'><dt id='teyGe'><q id='teyGe'><span id='teyGe'><b id='teyGe'><form id='teyGe'><ins id='teyGe'></ins><ul id='teyGe'></ul><sub id='teyGe'></sub></form><legend id='teyGe'></legend><bdo id='teyGe'><pre id='teyGe'><center id='teyGe'></center></pre></bdo></b><th id='teyGe'></th></span></q></dt></tr></i><div class="5jhph7n" id='teyGe'><tfoot id='teyGe'></tfoot><dl id='teyGe'><fieldset id='teyGe'></fieldset></dl></div>

      1. PowerMockito 模擬單個靜態(tài)方法并返回對象

        PowerMockito mock single static method and return object(PowerMockito 模擬單個靜態(tài)方法并返回對象)
        <i id='fUNNn'><tr id='fUNNn'><dt id='fUNNn'><q id='fUNNn'><span id='fUNNn'><b id='fUNNn'><form id='fUNNn'><ins id='fUNNn'></ins><ul id='fUNNn'></ul><sub id='fUNNn'></sub></form><legend id='fUNNn'></legend><bdo id='fUNNn'><pre id='fUNNn'><center id='fUNNn'></center></pre></bdo></b><th id='fUNNn'></th></span></q></dt></tr></i><div class="ftrphld" id='fUNNn'><tfoot id='fUNNn'></tfoot><dl id='fUNNn'><fieldset id='fUNNn'></fieldset></dl></div>

            <tbody id='fUNNn'></tbody>

                <tfoot id='fUNNn'></tfoot>

              • <legend id='fUNNn'><style id='fUNNn'><dir id='fUNNn'><q id='fUNNn'></q></dir></style></legend>

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

                  <bdo id='fUNNn'></bdo><ul id='fUNNn'></ul>

                • 本文介紹了PowerMockito 模擬單個靜態(tài)方法并返回對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我想從一個包含 2 個靜態(tài)方法 m1 和 m2 的類中模擬一個靜態(tài)方法 m1.我希望方法 m1 返回一個對象.

                  我嘗試了以下

                  1)

                  PowerMockito.mockStatic(Static.class, new Answer<Long>() {@覆蓋公共長答案(InvocationOnMock 調(diào)用)拋出 Throwable {返回 1000 升;}});

                  這同時調(diào)用了 m1 和 m2,它們具有不同的返回類型,因此它給出了返回類型不匹配錯誤.

                  2) PowerMockito.when(Static.m1(param1, param2)).thenReturn(1000l);但是在執(zhí)行 m1 時不會調(diào)用它.

                  3) PowerMockito.mockPartial(Static.class, "m1");給出了 mockPartial 不可用的編譯器錯誤,這是我從 http://code.google.com 獲得的/p/powermock/wiki/MockitoUsage.

                  解決方案

                  你想做的是1的一部分和2的全部組合.

                  您需要使用 PowerMockito.mockStatic 為類的所有靜態(tài)方法啟用靜態(tài)模擬.這意味著可以使用 when-thenReturn 語法對它們進(jìn)行存根.

                  但是,您使用的 mockStatic 的 2 參數(shù)重載為 Mockito/PowerMock 在調(diào)用未在模擬實(shí)例上顯式存根的方法時應(yīng)該執(zhí)行的操作提供了默認(rèn)策略.

                  來自 javadoc:p><塊引用>

                  創(chuàng)建具有指定策略的類模擬,以獲取其答案互動.這是相當(dāng)高級的功能,通常你不需要它編寫體面的測試.但是,在使用時可能會有所幫助遺留系統(tǒng).這是默認(rèn)答案,因此僅在以下情況下使用你不會存根方法調(diào)用.

                  默認(rèn)默認(rèn)存根策略是只為對象、數(shù)字和布爾值方法返回 null、0 或 false.通過使用 2-arg 重載,您是在說不,不,不,默認(rèn)情況下使用這個 Answer 子類的回答方法來獲取默認(rèn)值.它返回一個 Long,所以如果你有靜態(tài)方法返回不兼容的東西長,有問題.

                  相反,使用 1-arg 版本的 mockStatic 來啟用靜態(tài)方法的存根,然后使用 when-thenReturn 指定對特定方法執(zhí)行的操作.例如:

                  導(dǎo)入靜態(tài) org.mockito.Mockito.*;導(dǎo)入 org.junit.Test;導(dǎo)入 org.junit.runner.RunWith;導(dǎo)入 org.mockito.invocation.InvocationOnMock;導(dǎo)入 org.mockito.stubbing.Answer;導(dǎo)入 org.powermock.api.mockito.PowerMockito;導(dǎo)入 org.powermock.core.classloader.annotations.PrepareForTest;導(dǎo)入 org.powermock.modules.junit4.PowerMockRunner;類 ClassWithStatics {公共靜態(tài)字符串 getString() {返回字符串";}公共靜態(tài) int getInt() {返回 1;}}@RunWith(PowerMockRunner.class)@PrepareForTest(ClassWithStatics.class)公共類 StubJustOneStatic {@測試公共無效測試(){PowerMockito.mockStatic(ClassWithStatics.class);when(ClassWithStatics.getString()).thenReturn("你好!");System.out.println("字符串:" + ClassWithStatics.getString());System.out.println("Int:" + ClassWithStatics.getInt());}}

                  String 值靜態(tài)方法被存根返回Hello!",而 int 值靜態(tài)方法使用默認(rèn)存根,返回 0.

                  I want to mock a static method m1 from a class which contains 2 static methods, m1 and m2. And I want the method m1 to return an object.

                  I tried the following

                  1)

                  PowerMockito.mockStatic(Static.class, new Answer<Long>() {
                           @Override
                           public Long answer(InvocationOnMock invocation) throws Throwable {
                              return 1000l;
                           }
                        });
                  

                  This is calling both m1 and m2, which has a different return type, so it gives a return type mismatch error.

                  2) PowerMockito.when(Static.m1(param1, param2)).thenReturn(1000l); But this is not called when m1 is executed.

                  3) PowerMockito.mockPartial(Static.class, "m1"); Gives compiler error that mockPartial not available, which I got from http://code.google.com/p/powermock/wiki/MockitoUsage.

                  解決方案

                  What you want to do is a combination of part of 1 and all of 2.

                  You need to use the PowerMockito.mockStatic to enable static mocking for all static methods of a class. This means make it possible to stub them using the when-thenReturn syntax.

                  But the 2-argument overload of mockStatic you are using supplies a default strategy for what Mockito/PowerMock should do when you call a method you haven't explicitly stubbed on the mock instance.

                  From the javadoc:

                  Creates class mock with a specified strategy for its answers to interactions. It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems. It is the default answer so it will be used only when you don't stub the method call.

                  The default default stubbing strategy is to just return null, 0 or false for object, number and boolean valued methods. By using the 2-arg overload, you're saying "No, no, no, by default use this Answer subclass' answer method to get a default value. It returns a Long, so if you have static methods which return something incompatible with Long, there is a problem.

                  Instead, use the 1-arg version of mockStatic to enable stubbing of static methods, then use when-thenReturn to specify what to do for a particular method. For example:

                  import static org.mockito.Mockito.*;
                  
                  import org.junit.Test;
                  import org.junit.runner.RunWith;
                  import org.mockito.invocation.InvocationOnMock;
                  import org.mockito.stubbing.Answer;
                  import org.powermock.api.mockito.PowerMockito;
                  import org.powermock.core.classloader.annotations.PrepareForTest;
                  import org.powermock.modules.junit4.PowerMockRunner;
                  
                  class ClassWithStatics {
                    public static String getString() {
                      return "String";
                    }
                  
                    public static int getInt() {
                      return 1;
                    }
                  }
                  
                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest(ClassWithStatics.class)
                  public class StubJustOneStatic {
                    @Test
                    public void test() {
                      PowerMockito.mockStatic(ClassWithStatics.class);
                  
                      when(ClassWithStatics.getString()).thenReturn("Hello!");
                  
                      System.out.println("String: " + ClassWithStatics.getString());
                      System.out.println("Int: " + ClassWithStatics.getInt());
                    }
                  }
                  

                  The String-valued static method is stubbed to return "Hello!", while the int-valued static method uses the default stubbing, returning 0.

                  這篇關(guān)于PowerMockito 模擬單個靜態(tài)方法并返回對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)建一個隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)
                  <legend id='YXr4y'><style id='YXr4y'><dir id='YXr4y'><q id='YXr4y'></q></dir></style></legend>

                    • <tfoot id='YXr4y'></tfoot>

                          <bdo id='YXr4y'></bdo><ul id='YXr4y'></ul>
                            <tbody id='YXr4y'></tbody>

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

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

                            主站蜘蛛池模板: 工控机-图像采集卡-PoE网卡-人工智能-工业主板-深圳朗锐智科 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | PU树脂_水性聚氨酯树脂_聚氨酯固化剂_聚氨酯树脂厂家_宝景化工 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 国际高中-国际学校-一站式择校服务-远播国际教育 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 | 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | 出国劳务公司_正规派遣公司[严海]| 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 电线电缆厂家|沈阳电缆厂|电线厂|沈阳英联塑力线缆有限公司 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 河南砖机首页-全自动液压免烧砖机,小型砌块水泥砖机厂家[十年老厂] | 余姚生活网_余姚论坛_余姚市综合门户网站 | 济南电缆桥架|山东桥架-济南航丰实业有限公司 | 威廉希尔WilliamHill·足球(中国)体育官方网站 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 石膏基自流平砂浆厂家-高强石膏基保温隔声自流平-轻质抹灰石膏粉砂浆批发-永康市汇利建设有限公司 | 雄松华章(广州华章MBA)官网-专注MBA/MPA/MPAcc/MEM辅导培训 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 汝成内控-行政事业单位内部控制管理服务商| CE认证_产品欧盟ROHS-REACH检测机构-商通检测 | 污水处理设备,一体化泵站,一体化净水设备-「梦之洁环保设备厂家」 | 团建-拓展-拓展培训-拓展训练-户外拓展训练基地[无锡劲途] | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 活动策划,舞台搭建,活动策划公司-首选美湖上海活动策划公司 | 美侍宠物-专注宠物狗及宠物猫训练|喂养|医疗|繁育|品种|价格 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 不锈钢酒柜|恒温酒柜|酒柜定制|酒窖定制-上海啸瑞实业有限公司 | 不锈钢散热器,冷却翅片管散热器厂家-无锡市烨晟化工装备科技有限公司 | 双段式高压鼓风机-雕刻机用真空泵-绍兴天晨机械有限公司 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 |