Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use doReturn to partially mock static method with PowerMockito

How to I use the doReturn pattern in PowerMockito to mock a static method when I can't use Mockito.when()?

I want to test the following static method:

public static PrintWriter openWriter(File file, Charset charset, boolean autoflush) throws FileNotFoundException {
      return openWriterHelper(new FileOutputStream(file), charset, autoflush);
   }

This is my testMethod:

@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);

}

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.

If it matters, this is the code for openWriterHelper:

public static PrintWriter openWriterHelper(OutputStream stream, Charset charset,
                                    boolean autoflush) {
  return new PrintWriter(new java.io.BufferedWriter(
        new java.io.OutputStreamWriter(stream, charset)), autoflush);

}

like image 230
Zack Avatar asked Sep 12 '11 17:09

Zack


People also ask

How do you mock a static method in PowerMockito?

Use PowerMockito. mockStatic() for mocking class with static methods. Use PowerMockito. verifyStatic() for verifying mocked methods using Mockito.

Can we use Mockito and PowerMock together?

Of course you can – and probably will – use Mockito and PowerMock in the same JUnit test at some point of time.

How do you mock with PowerMock?

To use PowerMock with Mockito, we need to apply the following two annotations in the test: @RunWith(PowerMockRunner. class): It is the same as we have used in our previous examples. The only difference is that in the previous example we have used MockitoUnitRunner.

What is the difference between doReturn and thenReturn?

The parameter of doReturn is Object unlike thenReturn . So, there is no type checking in the compile time. When the type is mismatched in the runtime, there would be an WrongTypeOfReturnValue execption. doReturn(true).


2 Answers

try

doReturn(expected).when(IOHelper.class, "openWriterHelper", file, charset, true);

or

when(IOHelper.class, "openWriterHelper", file, charset, true).thenReturn(expected);

see samples in: http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/powermockito/junit4/src/test/java/samples/powermockito/junit4/partialmocking/StaticPartialMockingTest.java?r=1366

like image 121
ejaenv Avatar answered Oct 07 '22 16:10

ejaenv


Replace this line of code:

when(IOHelper.openWriterHelper(fos, charset, true)).thenReturn(expected);

with

 doReturn(expected).when(IOHelper.class);
 IOHelper.openWriter(fos,charset, true);
like image 45
Zack Avatar answered Oct 07 '22 18:10

Zack