title: Mocking static methods with Powermockito
body: <p>I am trying to mock static method with <code>PowerMockito</code>. I have referred to various stackoverflow answers such as this:<a href="https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito%20.">Mocking static methods with PowerMock and Mockito</a></p>
  
  <p>But I am getting : <code>org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
  Misplaced argument matcher detected here</code> exception. 
  I have spent hours debugging my code and google searching  but to no avail. What am I missing here?</p>
  
  <p><strong>* Note: I am running it as a testng test. *</strong></p>
  
  <p>I have added comments in my code that may help you understand what I am trying to do.</p>
  
  <p>The full stack trace is:</p>
  
  <pre><code> org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
   Misplaced argument matcher detected here:
  
   at MyClassTest.testMethod1(MyClassTest.java:24)`
  
   You cannot use argument matchers outside of verification or stubbing.
   Examples of correct usage of argument matchers: 
      when(mock.get(anyInt())).thenReturn(null);
      doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
      verify(mock).someMethod(contains("foo")) `
  
   Also, this error might show up because you use argument matchers with methods that cannot be mocked.
   Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode(). 
   Mocking methods declared on non-public parent classes is not supported.`
  
  at MyClassTest.testMethod1(MyClassTest.java:24)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
  at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
  at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
  at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
  at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
  at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
  at org.testng.TestRunner.privateRun(TestRunner.java:767)
  at org.testng.TestRunner.run(TestRunner.java:617)
  at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
  at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
  at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
  at org.testng.SuiteRunner.run(SuiteRunner.java:254)
  at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
  at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
  at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
  at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
  at org.testng.TestNG.run(TestNG.java:1057)
  at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
  at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
  at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
  </code></pre>
  
  <p>`</p>
  
  <p>Below is my code:</p>
  
  <blockquote>
    <p>Test Class</p>
  </blockquote>
  
  <pre><code>import static org.mockito.Matchers.anyString;
  import org.junit.runner.RunWith;
  import org.mockito.BDDMockito;
  import org.mockito.Mockito;
  import org.powermock.api.mockito.PowerMockito;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  import org.testng.annotations.Test;
  
  @Test
  @RunWith(PowerMockRunner.class)
  @PrepareForTest(AnotherClass.class)
  public class MyClassTest {
  
      MyClass myClass;
  
      @Test
      public void testMethod1() {
  
          /*
           * Mock static methods
           */
          PowerMockito.mockStatic(AnotherClass.class);
          BDDMockito.given(AnotherClass.yetAnotherMethod(anyString())).willReturn(Mockito.mock(String.class));
  
          // call the method of system under test
          myClass.method1();
  
          // verify
          PowerMockito.verifyStatic();
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>System Under Test:</p>
  </blockquote>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  public class MyClass {
  
      public String method1() {
          String result = AnotherClass.yetAnotherMethod("Pramithas");
          return result;
      }
  }
  </code></pre>
  
  <blockquote>
    <p><code>AnotherClass.java</code>:</p>
  </blockquote>
  
  <pre><code>import org.springframework.stereotype.Service;
  
  public class AnotherClass {
  
      public static String yetAnotherMethod(String s) {
          return s;
      }
  }
  </code></pre>