5

I want to stub the public static function readAllBytes from java.nio.file.Files with the following test code.

@PrepareForTest(Files.class)
public void testGetNotExistingRestFile() throws Exception {
    PowerMockito.mockStatic(Files.class);
    PowerMockito.doThrow(mock(IOException.class)).when(Files.readAllBytes(any(Path.class)));
}

Every time an NullPointerException is thrown and I can figure out what I'm doing wrong.

java.lang.NullPointerException
at java.nio.file.Files.provider(Files.java:67)
at java.nio.file.Files.newByteChannel(Files.java:317)
at java.nio.file.Files.newByteChannel(Files.java:363)
at java.nio.file.Files.readAllBytes(Files.java:2981)
at nl.mooij.bob.RestFileProviderTest.testGetNotExistingRestFile(RestFileProviderTest.java:53)

How can I stub the function readAllBytes from java.nio.file.Files with PowerMockito?

bmooij
  • 142
  • 2
  • 9
  • 1
    Are you using [`@PrepareForTest`](http://powermock.googlecode.com/svn-history/r1254/docs/powermock-1.3.5/apidocs/org/powermock/core/classloader/annotations/PrepareForTest.html) ? – khelwood Apr 11 '15 at 21:27
  • There's some guidance about mocking static methods here: https://code.google.com/p/powermock/wiki/MockStatic – khelwood Apr 11 '15 at 21:29
  • It might not be related to your problem, but is there a reason you throw a mock `IOException` instead of just `doThrow(IOException.class)` ? – khelwood Apr 11 '15 at 21:38
  • doThrow (java.lang.Throwable) in PowerMockito cannot be applied to (java.lang.Class ) – bmooij Apr 11 '15 at 21:46

3 Answers3

3

Call Mockito, instead of PowerMockito and reverse the stubbing order:

@Test(expected=IOException.class)
@PrepareForTest(Files.class)
public void testGetNotExistingRestFile() throws Exception {
    // arrange
     PowerMockito.mockStatic(Files.class);
     Mockito.when(Files.readAllBytes(Matchers.any(Path.class))).thenThrow(Mockito.mock(IOException.class));
    // act
     Files.readAllBytes(Mockito.mock(Path.class));
}

Another possibility is:

   @Test(expected=IOException.class)
   @PrepareForTest(Files.class)
   public void testGetNotExistingRestFile() throws Exception {
     // arrange
       PowerMockito.mockStatic(Files.class);
       Files filesMock = PowerMockito.mock(Files.class);
       Mockito.when(filesMock.readAllBytes(Matchers.any(Path.class))).thenThrow(Mockito.mock(IOException.class));
     // act   
       filesMock.readAllBytes(Mockito.mock(Path.class));
   }

Reference: Using PowerMockito to Mock Final and Static Methods

Jose Tepedino
  • 1,524
  • 15
  • 18
  • For some reason I had to have `@PrepareForTest(Files.class, ClassThatCallsFiles.class)`, but otherwise it works. – Ben Watson Feb 12 '18 at 13:56
3

Make sure you are including the class that calls the static method in your @PrepareForTest.

@PrepareForTest({Files.class, ClassThatCallsFiles.class})
nickb
  • 59,313
  • 13
  • 108
  • 143
0

Please add this dependency in pom.xml While Mocking Files class for static method.

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-core</artifactId>
    <version>2.0.9</version>
    <scope>test</scope>
</dependency>

This is also one of the factor you are getting NullPointerException