title: Powermock : Mocked method still called
body: <p>First of all, please know that I've searched SO before asking this question, but I was unable to find a satisfying answer.</p>
  
  <p>I'm using JUnit4 and Powermock 1.5.5 (with mockito 1.9.5)</p>
  
  <p>My problem is the following : in my unit tests, I need to mock a static method in a class I can't modify. I only want to mock one method, and not the whole class, so I went for a spy.</p>
  
  <p>Here's what I have so far :</p>
  
  <pre><code>[...]
  import static org.mockito.Matchers.*;
  import static org.powermock.api.mockito.PowerMockito.*;
  
  @RunWith(JUnitParamsRunner.class)
  @ContextConfiguration(locations={"classpath:applicationContext-test.xml"},
      loader=MockWebApplicationContextLoader.class)
  @MockWebApplication(name="my-app")
  @PrepareForTest(value = {
      Role.class
  })
  public class MyTest {
  
      @Rule
      public PowerMockRule powerMockRule = new PowerMockRule();
  
      @Before
      public void setUp() throws Exception {
          initSpring();
          mockRoleServices();
      }
  
      private void mockRoleServices() throws Exception {
          spy(Role.class);
          RoleAnswer roleAnswer = new RoleAnswer(RoleEnum.ADMIN);
          when(Role.hasAdministratorRole(anyLong(), anyLong(), anyLong()))
              .then(roleAnswer);
      }
  
      private class RoleAnswer implements Answer&lt;Boolean&gt; {
  
          private RoleEnum roleEnum;
  
          private RoleAnswer(RoleEnum roleEnum) {
              this.roleEnum = roleEnum;
          }
  
          @Override
          public Boolean answer(InvocationOnMock invocation) throws Throwable {
              return getRenderRequest().getUserRole() != null &amp;&amp;
                      getRenderRequest().getUserRole().equals(roleEnum);
          }
      }
  }
  </code></pre>
  
  <p>Here's the problem : the method <code>Role.hasAdministratorRole()</code> is called instead of being mocked</p>
  
  <p>Here's what I tried so far :</p>
  
  <ul>
  <li>Using <code>mockStatic(Role.class)</code> instead of the <code>spy()</code> method. As expected, all methods are mocked, so I end up getting an NPE before <code>Role.hasAdministratorRole()</code> is called</li>
  <li>Doing something like <code>doAnswer(...).when(...)</code>. I get a runtime error with powermock telling me my mock is not complete (which actually confirms that something's wrong either with my code or with the lib itself)</li>
  <li>Trying to declare the method by its name rather than calling it directly : <code>when(Role.class, "hasAdministratorRole", long.class, long.class, long.class)</code>. Same behavior</li>
  <li>A bunch of other things I don't recall anymore.</li>
  </ul>
  
  <p>Your help will be greatly appreciated.
  Thanks !</p>
  
  <p><strong>EDIT</strong> : Thanks to SrikanthLingala's answer, I was able to pinpoint the problem.</p>
  
  <p>This didn't work :</p>
  
  <pre><code>when(Role.hasAdministratorRole(anyLong(), anyLong(), anyLong()))
      .thenAnswer(roleAnswer);
  </code></pre>
  
  <p>but this did :</p>
  
  <pre><code>doAnswer(roleAnswer).when(Role.class, "hasSiteAdministratorRole",
      anyLong(), anyLong(), anyLong());
  </code></pre>
  
  <p>So switching then <code>when()</code> and the <code>answer()</code> worked</p>