Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to use partial mocking for private static methods in PowerMock?
    primarykey
    data
    text
    <p>From the examples on the <a href="http://code.google.com/p/powermock/wiki/MockitoUsage13#A_full_example_of_partial_mocking_of_a_private_method%20PowerMock%20website">PowerMock homepage</a>, I see the following example for partially mocking a private method with Mockito:</p> <pre><code>@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"); } </code></pre> <p>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:</p> <pre><code>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&lt;String&gt; getData() { List&lt;String&gt; data = null; try { data = readFile(); } catch (IOException e) { e.printStackTrace(); } return data; } private static List&lt;String&gt; readFile() throws IOException { File file = new File("/some/path/to/file"); List&lt;String&gt; lines = readLines(file, Charset.forName("utf-8")); return lines; } } </code></pre> <p>Please could someone let me know how this can be achieved?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload