title: PowerMockito mock static method which throws exception
body: <p>I have some static methods to mock using Mockito + PowerMock. Everything was correct until  I tried to mock a static method which throws exception only (and do nothing else).</p>
  
  <p>My test class looks like this:</p>
  
  <p>top:</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({Secure.class, User.class, StringUtils.class})
  </code></pre>
  
  <p>body:</p>
  
  <pre><code>    PowerMockito.mockStatic(Secure.class);
      Mockito.when(Secure.getCurrentUser()).thenReturn(user);
  
      PowerMockito.mockStatic(StringUtils.class);
      Mockito.when(StringUtils.isNullOrEmpty("whatever")).thenReturn(true);
  
      PowerMockito.mockStatic(User.class);
      Mockito.when(User.findById(1L)).thenReturn(user); // exception !! ;(
  
      boolean actualResult = service.changePassword();
  </code></pre>
  
  <p>and changePassword method is:</p>
  
  <pre><code>  Long id = Secure.getCurrentUser().id;
  
    boolean is = StringUtils.isNullOrEmpty("whatever");
  
    User user = User.findById(1L);
    // ...
  </code></pre>
  
  <p>The first 2 static calls works fine (if i comment out 3rd), but the last one ( User.findById(long id) ) throws exception while it is called in 'Mockito.when' method. This method looks like this:</p>
  
  <pre><code> public static &lt;T extends JPABase&gt; T findById(Object id) {
          throw new UnsupportedOperationException("Please annotate your JPA model with @javax.persistence.Entity annotation.");
      }
  </code></pre>
  
  <p>My question is how can i mock this method to get result as  I expect ?
  Thanks for any help.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Thanks for all replies. I found a solution. I was trying to mock a method findById which was not directly in User.class but in GenericModel.class which User extends. Now everything works perfectly. </p>