title: Powermockito - cannot mock/spy a system class
body: <p>I am trying to mock a static method in the <code>java.nio.file.Files</code> class like this:</p>
  
  <pre><code>import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  
  import static org.powermock.api.mockito.PowerMockito.mockStatic;
  import static org.powermock.api.mockito.PowerMockito.when;
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest({Files.class})
  public class MockStaticTest {
  
      @Test
      public void test() throws IOException {
          Path absolutePath = Paths.get("AB", "CD", "EF");
          mockStatic(Files.class);
          when(Files.createDirectories(absolutePath)).thenReturn(null);
      }
  }
  </code></pre>
  
  <p>When I execute the test class, I get this exception:</p>
  
  <pre><code>org.mockito.exceptions.base.MockitoException: 
  Cannot mock/spy class java.nio.file.Files
  Mockito cannot mock/spy because :
   - final class
  
      at MockStaticTest.test(MockStaticTest.java:22)
      at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
      at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
      at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
      at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
      at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
      at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  
  
  Process finished with exit code -1
  </code></pre>
  
  <p>However, if I try to mock any other final class, it works without any issues, for ex:</p>
  
  <pre><code>import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  
  import static org.powermock.api.mockito.PowerMockito.mockStatic;
  import static org.powermock.api.mockito.PowerMockito.when;
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest({Files.class, NettyChannelBuilder.class})
  public class MockStaticTest {
  
      @Test
      public void test() throws IOException {
          Path absolutePath = Paths.get("AB", "CD", "EF");
  //        mockStatic(Files.class);
          mockStatic(NettyChannelBuilder.class);
  //        when(Files.createDirectories(absolutePath)).thenReturn(null);
          when(NettyChannelBuilder.forAddress("", 0)).thenReturn(null);
      }
  }
  </code></pre>
  
  <p>Could someone please help me out with this ? Why the <code>Files</code> class is throwing a mock exception ?</p>
  
  <p>Maven dependencies:</p>
  
  <pre><code>         &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-api-mockito2&lt;/artifactId&gt;
              &lt;version&gt;2.0.2&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
            &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.mockito&lt;/groupId&gt;
        &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
        &lt;version&gt;2.28.2&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>