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

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

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

      2. <tfoot id='QwfO7'></tfoot>

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

        是否可以在 PowerMock 中對私有靜態方法使用部分模

        Is it possible to use partial mocking for private static methods in PowerMock?(是否可以在 PowerMock 中對私有靜態方法使用部分模擬?)
        <i id='gCuEA'><tr id='gCuEA'><dt id='gCuEA'><q id='gCuEA'><span id='gCuEA'><b id='gCuEA'><form id='gCuEA'><ins id='gCuEA'></ins><ul id='gCuEA'></ul><sub id='gCuEA'></sub></form><legend id='gCuEA'></legend><bdo id='gCuEA'><pre id='gCuEA'><center id='gCuEA'></center></pre></bdo></b><th id='gCuEA'></th></span></q></dt></tr></i><div class="ukaekqm" id='gCuEA'><tfoot id='gCuEA'></tfoot><dl id='gCuEA'><fieldset id='gCuEA'></fieldset></dl></div>
        <legend id='gCuEA'><style id='gCuEA'><dir id='gCuEA'><q id='gCuEA'></q></dir></style></legend><tfoot id='gCuEA'></tfoot>

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

              <tbody id='gCuEA'></tbody>

            • <bdo id='gCuEA'></bdo><ul id='gCuEA'></ul>
                1. 本文介紹了是否可以在 PowerMock 中對私有靜態方法使用部分模擬?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  從 PowerMock 主頁上的示例中,我看到了以下示例使用 Mockito 部分模擬私有方法:

                  From the examples on the PowerMock homepage, I see the following example for partially mocking a private method with Mockito:

                  @RunWith(PowerMockRunner.class)
                  // We prepare PartialMockClass for test because it's final or we need to mock private or static methods
                  @PrepareForTest(PartialMockClass.class)
                  public class YourTestCase {
                  @Test
                  public void privatePartialMockingWithPowerMock() {        
                      PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());
                  
                      // use PowerMockito to set up your expectation
                      PowerMockito.doReturn(value).when(classUnderTest, "methodToMock", "parameter1");
                  
                      // execute your test
                      classUnderTest.execute();
                  
                      // Use PowerMockito.verify() to verify result
                      PowerMockito.verifyPrivate(classUnderTest, times(2)).invoke("methodToMock", "parameter1");
                  }
                  

                  但是,當我們希望模擬的私有方法是靜態的時,這種方法似乎不起作用.我希望創建以下類的部分模擬,并模擬 readFile 方法:

                  However, this approach does not seem to work when the private method we wish to mock is static. I wish to create a partial mock of the below class, with the readFile method mocked:

                  package org.rich.powermockexample;
                  
                  import java.io.File;
                  import java.io.IOException;
                  import java.nio.charset.Charset;
                  import java.util.List;
                  
                  import static com.google.common.io.Files.readLines;
                  
                  public class DataProvider {
                  
                      public static List<String> getData() {
                          List<String> data = null;
                          try {
                              data = readFile();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                          return data;
                      }
                  
                      private static List<String> readFile() throws IOException {
                          File file = new File("/some/path/to/file");
                          List<String> lines = readLines(file, Charset.forName("utf-8"));
                          return lines;
                      }
                  
                  }
                  

                  請有人告訴我這是如何實現的嗎?

                  Please could someone let me know how this can be achieved?

                  推薦答案

                  經過一番研究,這里似乎需要 PowerMockito.spy() 和 PowerMockito.doReturn() :

                  After doing a bit more research, it seems that PowerMockito.spy() and PowerMockito.doReturn() are what is required here:

                  package com.richashworth.powermockexample;
                  
                  import org.junit.Before;
                  import org.junit.BeforeClass;
                  import org.junit.Test;
                  import org.junit.runner.RunWith;
                  import org.powermock.api.mockito.PowerMockito;
                  import org.powermock.core.classloader.annotations.PrepareForTest;
                  import org.powermock.modules.junit4.PowerMockRunner;
                  
                  import java.util.ArrayList;
                  import java.util.List;
                  
                  import static org.junit.Assert.assertEquals;
                  
                  
                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest({DataProvider.class})
                  public class ResultsWriterTest {
                  
                      private static List<String> mockData = new ArrayList<String>();
                      private ResultsWriter resultsWriter;
                  
                      @BeforeClass
                      public static void setUpOnce() {
                          final String firstLine = "Line 1";
                          final String secondLine = "Line 2";
                          mockData.add(firstLine);
                          mockData.add(secondLine);
                      }
                  
                      @Before
                      public void setUp() {
                          resultsWriter = new ResultsWriter();
                      }
                  
                      @Test
                      public void testGetDataAsString() throws Exception {
                          PowerMockito.spy(DataProvider.class);
                          PowerMockito.doReturn(mockData).when(DataProvider.class, "readFile");
                  
                          final String expectedData = "Line 1
                  Line 2
                  ";
                          final String returnedString = resultsWriter.getDataAsString();
                  
                          assertEquals(expectedData, returnedString);
                      }
                  
                  }
                  

                  有關更多詳細信息和完整代碼清單,請在此處查看我的博客文章:https://richashworth.com/post/turbocharge-your-mocking-framework-with-powermock/

                  For further details and the complete code listing, check out my blog post here: https://richashworth.com/post/turbocharge-your-mocking-framework-with-powermock/

                  這篇關于是否可以在 PowerMock 中對私有靜態方法使用部分模擬?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

                2. <legend id='WkPQr'><style id='WkPQr'><dir id='WkPQr'><q id='WkPQr'></q></dir></style></legend>
                    <bdo id='WkPQr'></bdo><ul id='WkPQr'></ul>
                      1. <i id='WkPQr'><tr id='WkPQr'><dt id='WkPQr'><q id='WkPQr'><span id='WkPQr'><b id='WkPQr'><form id='WkPQr'><ins id='WkPQr'></ins><ul id='WkPQr'></ul><sub id='WkPQr'></sub></form><legend id='WkPQr'></legend><bdo id='WkPQr'><pre id='WkPQr'><center id='WkPQr'></center></pre></bdo></b><th id='WkPQr'></th></span></q></dt></tr></i><div class="muuwwse" id='WkPQr'><tfoot id='WkPQr'></tfoot><dl id='WkPQr'><fieldset id='WkPQr'></fieldset></dl></div>

                          <tfoot id='WkPQr'></tfoot>

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

                              <tbody id='WkPQr'></tbody>

                            主站蜘蛛池模板: 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | POS机办理_个人pos机免费领取-银联pos机申请首页 | 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | 中医中药治疗血小板减少-石家庄血液病肿瘤门诊部 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 净水器代理,净水器招商,净水器加盟-FineSky德国法兹全屋净水 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | 至顶网| 防火卷帘门价格-聊城一维工贸特级防火卷帘门厂家▲ | 自动焊锡机_点胶机_螺丝机-锐驰机器人| 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | 江苏南京多语种翻译-专业翻译公司报价-正规商务翻译机构-南京华彦翻译服务有限公司 | 澳威全屋定制官网|极简衣柜十大品牌|衣柜加盟代理|全屋定制招商 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 液氮罐_液氮容器_自增压液氮罐-北京君方科仪科技发展有限公司 | 衬塑设备,衬四氟设备,衬氟设备-淄博鲲鹏防腐设备有限公司 | 旋片真空泵_真空泵_水环真空泵_真空机组-深圳恒才机电设备有限公司 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 深圳南财多媒体有限公司介绍| 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | 楼承板-开闭口楼承板-无锡海逵楼承板 | pH污水传感器电极,溶解氧电极传感器-上海科蓝仪表科技有限公司 | 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 上海诺狮景观规划设计有限公司 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 保温杯,儿童婴童奶瓶,运动水壶「广告礼品杯定制厂家」超朗保温杯壶 | 周易算网-八字测算网 - 周易算网-宝宝起名取名测名字周易八字测算网 | 定时排水阀/排气阀-仪表三通旋塞阀-直角式脉冲电磁阀-永嘉良科阀门有限公司 |