title: Use doReturn to partially mock static method with PowerMockito
body: <p>How to I use the doReturn pattern in PowerMockito to mock a static method when I can't use Mockito.when()?</p>
  
  <p>I want to test the following static method:</p>
  
  <pre><code>public static PrintWriter openWriter(File file, Charset charset, boolean autoflush) throws FileNotFoundException {
        return openWriterHelper(new FileOutputStream(file), charset, autoflush);
     }
  </code></pre>
  
  <p>This is my testMethod:</p>
  
  <pre><code>@Test
  public void testOpenWriter_file_charset_autoflush() throws Throwable {
        Charset charset = mock(Charset.class);
        PrintWriter expected = mock(PrintWriter.class);
        File file = mock(File.class);
        FileOutputStream fos = mock(FileOutputStream.class);
  
        spy(IOHelper.class);
        whenNew(FileOutputStream.class).withArguments(file).thenReturn(fos);
        when(IOHelper.openWriterHelper(fos, charset, true)).thenReturn(expected);
  
        PrintWriter observed = IOHelper.openWriter(file, charset, true);
        assertEquals(expected, observed);
  
        verifyStatic();
        IOHelper.openWriterHelper(fos, charset, true);
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is that I can't put openWriterHelper in a call to when, because the method will raise an exception when passed a mock OutputStream.</p>
  
  <p>If it matters, this is the code for openWriterHelper: </p>
  
  <pre><code>public static PrintWriter openWriterHelper(OutputStream stream, Charset charset,
                                      boolean autoflush) {
    return new PrintWriter(new java.io.BufferedWriter(
          new java.io.OutputStreamWriter(stream, charset)), autoflush);
  </code></pre>
  
  <p>}</p>