title: Mocking the static method with Mockito
body: <p>I am trying to mock static method using powermock.
  Below is my code:</p>
  
  <pre><code>public class Helper{
  
    public static User getLoggedInUser(HttpServletRequest request) throws NotFoundException {
      String access = request.getHeader("Authorization");
      if(access == null || access.isEmpty()) {
        throw new Exception("Access is null");
      }
      User user = new User();
      return user;
  
    }
  
  }
  </code></pre>
  
  <p>And this is the controller function from where i am calling the static method getUser:</p>
  
  <pre><code>@RequestMapping(value = "user/userInfo/{Id}", method = RequestMethod.GET, headers = "Accept=application/json")
      public @ResponseBody
      ResultDTO getUser(@PathVariable("Id") Integer Id, HttpServletRequest request) throws NotFoundException, UnauthorizedException {
  
          Integer userID = -1;
  
             User user = Helper.getLoggedInUser(request);
             if(user != null){
                  userID = user.getUserId();
             }
  
          //do something
      }
  </code></pre>
  
  <p>And this is my test class:</p>
  
  <pre><code>//@RunWith(PowerMockRunner.class)
  //@PrepareForTest(Helper.class)
  public class CustomerControllerNGTest {
  
  @InjectMocks
      private userController instance = new PaymentCustomerController();
      public PaymentCustomerControllerNGTest() {
      }
  
      @BeforeClass
      public void setUpClass() throws Exception {
      }
  
      @AfterClass
      public static void tearDownClass() throws Exception {
      }
  
      @BeforeMethod
      public void setUpMethod() throws Exception {
  
          try{
              MockitoAnnotations.initMocks(this);
          }catch(Exception ex){
             System.out.println(ex.getMessage()); 
          }
          try{
          mockMvc = MockMvcBuilders.standaloneSetup(instance).build();
             // mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
          }catch(Exception ex){
              System.out.println(ex.getMessage());
          }
      }
  
      @AfterMethod
      public void tearDownMethod() throws Exception {
      }
  
      @Test
      public void testGetUserInfo() throws Exception {
          User user = new  User();
          user.setUserId(1234);
          HttpServletRequest request = mock(HttpServletRequest.class);
  
          //this is for the static method
          PowerMockito.mockStatic(Helper.class);
          **PowerMockito.when(Helper.getLoggedInUser(request)).thenReturn(user);**
          //do something
  
  
      }
  
  }
  </code></pre>
  
  <p>Now whenever i am executing the test case, and whenever it is executing the lone marked with bold, it is going inside the static method and throwing the exception "Access is null" rather than mocking the method , it is executing the method. Any idea?
  I also tried by uncommenting these lines:</p>
  
  <pre><code>//@RunWith(PowerMockRunner.class)
  //@PrepareForTest(Helper.class)
  </code></pre>
  
  <p>but still same exception.
  Thanks</p>