久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<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="qwawimqqmiuu" 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 中對(duì)私有靜態(tài)方法使用部分模

        Is it possible to use partial mocking for private static methods in PowerMock?(是否可以在 PowerMock 中對(duì)私有靜態(tài)方法使用部分模擬?)
        <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="qwawimqqmiuu" 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 中對(duì)私有靜態(tài)方法使用部分模擬?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  從 PowerMock 主頁(yè)上的示例中,我看到了以下示例使用 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");
                  }
                  

                  但是,當(dāng)我們希望模擬的私有方法是靜態(tài)的時(shí),這種方法似乎不起作用.我希望創(chuàng)建以下類(lèi)的部分模擬,并模擬 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;
                      }
                  
                  }
                  

                  請(qǐng)有人告訴我這是如何實(shí)現(xiàn)的嗎?

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

                  推薦答案

                  經(jīng)過(guò)一番研究,這里似乎需要 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);
                      }
                  
                  }
                  

                  有關(guān)更多詳細(xì)信息和完整代碼清單,請(qǐng)?jiān)诖颂幉榭次业牟┛臀恼?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/

                  這篇關(guān)于是否可以在 PowerMock 中對(duì)私有靜態(tài)方法使用部分模擬?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語(yǔ)句之前的局部變量,這有關(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?)

                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="qwawimqqmiuu" 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>

                            主站蜘蛛池模板: 久久网亚洲 | 精品国产乱码一区二区三 | 999久久久| 在线亚洲欧美 | 国产成人免费视频网站高清观看视频 | 一区不卡在线观看 | 亚洲第一天堂无码专区 | 国产精品不卡一区 | 欧美一区二区在线观看 | 欧美综合一区 | 久久精品日产第一区二区三区 | 欧美视频成人 | 91精品国产综合久久久久久漫画 | 久久亚洲一区 | 51ⅴ精品国产91久久久久久 | 日韩欧美三区 | 日本黄色大片免费 | 一区二区高清在线观看 | 99国产在线 | 一级在线免费观看 | 天天综合网7799精品 | 久久91av | 亚洲成人毛片 | 国产精品不卡 | 青青伊人久久 | 在线一区视频 | 久久网一区二区 | 中文视频在线 | 999免费视频 | 黑人一级黄色大片 | 精品国产一区二区三区免费 | 欧美日韩在线观看一区二区三区 | 黄瓜av | 亚洲国产精品一区二区三区 | 日韩在线欧美 | 在线亚洲免费视频 | 狠狠色网 | 久久久久久久国产精品影院 | 午夜成人免费视频 | 在线视频91 | 日韩一区二区三区在线视频 |