久久久久久久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 中對私有靜態方法使用部分模

        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="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 中對私有靜態方法使用部分模擬?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

                            主站蜘蛛池模板: 亚洲精品一区二区三区在线 | 欧美日韩不卡合集视频 | 丝袜毛片| 国产欧美日韩在线 | 一级无毛片 | 日韩毛片| 亚洲国产一区视频 | 亚洲成人在线免费 | 黄色片av | 欧美激情在线观看一区二区三区 | 亚av在线 | 91精品久久久 | 91国在线视频 | 老司机免费视频 | xxxxxx国产| 日韩资源 | 亚洲人在线 | 夜夜操天天艹 | 国产精品日韩一区 | 日韩欧美一区二区在线播放 | 人人性人人性碰国产 | 久久久久久久久国产精品 | 五月天综合影院 | 人人性人人性碰国产 | 国产精品九九九 | 日韩 欧美 综合 | 欧美精品乱码久久久久久按摩 | 波霸ol一区二区 | 麻豆国产精品777777在线 | 国产一卡二卡三卡 | 精品国产乱码久久久久久图片 | 欧美日韩激情 | 亚洲 中文 欧美 日韩 在线观看 | 日本高清不卡视频 | 日本不卡高字幕在线2019 | 色视频在线免费观看 | 久久精品亚洲精品 | 欧美1—12sexvideos | 日韩成人精品 | 国产亚洲一区二区三区在线 | 国产精品乱码一区二区三区 |