<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 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'>

      • <bdo id='gCuEA'></bdo><ul id='gCuEA'></ul>
      1. 是否可以在 PowerMock 中对私有静态方法使用部分模

        时间:2023-09-26

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

          <tbody id='QwfO7'></tbody>

          • <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 id='QwfO7'><tfoot id='QwfO7'></tfoot><dl id='QwfO7'><fieldset id='QwfO7'></fieldset></dl></div>

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

                  <legend id='QwfO7'><style id='QwfO7'><dir id='QwfO7'><q id='QwfO7'></q></dir></style></legend>
                  本文介绍了是否可以在 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模板网!

                  上一篇:例外:mockito 想要但没有被调用,实际上与这个 下一篇:如何使用 PowerMockito 模拟私有静态方法?

                  相关文章

                  最新文章

                • <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 id='WkPQr'><tfoot id='WkPQr'></tfoot><dl id='WkPQr'><fieldset id='WkPQr'></fieldset></dl></div>

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

                    1. <small id='WkPQr'></small><noframes id='WkPQr'>