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="26d73wt" id='teyGe'><tfoot id='teyGe'></tfoot><dl id='teyGe'><fieldset id='teyGe'></fieldset></dl></div>

      1. PowerMockito 模擬單個靜態方法并返回對象

        PowerMockito mock single static method and return object(PowerMockito 模擬單個靜態方法并返回對象)
        <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="ggqavxo" 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 模擬單個靜態方法并返回對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

                  我嘗試了以下

                  1)

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

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

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

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

                  解決方案

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

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

                  但是,您使用的 mockStatic 的 2 參數重載為 Mockito/PowerMock 在調用未在模擬實例上顯式存根的方法時應該執行的操作提供了默認策略.

                  來自 javadoc:p><塊引用>

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

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

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

                  導入靜態 org.mockito.Mockito.*;導入 org.junit.Test;導入 org.junit.runner.RunWith;導入 org.mockito.invocation.InvocationOnMock;導入 org.mockito.stubbing.Answer;導入 org.powermock.api.mockito.PowerMockito;導入 org.powermock.core.classloader.annotations.PrepareForTest;導入 org.powermock.modules.junit4.PowerMockRunner;類 ClassWithStatics {公共靜態字符串 getString() {返回字符串";}公共靜態 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 值靜態方法被存根返回Hello!",而 int 值靜態方法使用默認存根,返回 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.

                  這篇關于PowerMockito 模擬單個靜態方法并返回對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)
                  <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="nkbwypg" id='YXr4y'><tfoot id='YXr4y'></tfoot><dl id='YXr4y'><fieldset id='YXr4y'></fieldset></dl></div>

                            主站蜘蛛池模板: 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 电动葫芦-河北悍象起重机械有限公司 | 医用酒精_84消毒液_碘伏消毒液等医用消毒液-漓峰消毒官网 | 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 聚丙烯酰胺_厂家_价格-河南唐达净水材料有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 聚丙烯酰胺_厂家_价格-河南唐达净水材料有限公司 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 开云(中国)Kaiyun·官方网站-登录入口 | 球磨机 选矿球磨机 棒磨机 浮选机 分级机 选矿设备厂家 | 中国玩具展_玩具展|幼教用品展|幼教展|幼教装备展 | ph计,实验室ph计,台式ph计,实验室酸度计,台式酸度计 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 建筑工程资质合作-工程资质加盟分公司-建筑资质加盟 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 宜兴市恺瑞德环保科技有限公司| 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 萃取箱-萃取槽-PVC萃取箱厂家-混合澄清槽- 杭州南方化工设备 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 智能电表|预付费ic卡水电表|nb智能无线远传载波电表-福建百悦信息科技有限公司 | 爆破器材运输车|烟花爆竹运输车|1-9类危险品厢式运输车|湖北江南专用特种汽车有限公司 | 杭州代理记账费用-公司注销需要多久-公司变更监事_杭州福道财务管理咨询有限公司 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 吉林污水处理公司,长春工业污水处理设备,净水设备-长春易洁环保科技有限公司 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛调查出轨取证公司_青岛婚外情取证-青岛探真调查事务所 | 山东包装,山东印刷厂,济南印刷厂-济南富丽彩印刷有限公司 | 工业铝型材-铝合金电机壳-铝排-气动执行器-山东永恒能源集团有限公司 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 |