title: Mock throwing a null pointer exception. New to mockito
body: <p>I'm trying to test the following method</p>
  
  <pre><code>  MainClass {
      ....
          Client client;
          WebTarget target;
          boolean doLogin(MultivaluedMap&lt;String, String&gt; headers) {
  
               client = getRestClient();
               target = client.target(BASE_URL))
                      .path("v1/login");
                MultivaluedMap&lt;String, Object&gt; castedHeaders = castMap(headers);//casts headers by entry.
                Response loginRsp = target
                      .request().headers(castedHeaders)     
                      .post(Entity.entity(buildIusLoginEntity(),
                                          MediaType.APPLICATION_JSON_TYPE));
                if (loginRsp.getStatus() != HttpServletResponse.SC_OK) {
  
                      return false;
                }
                return true;
          }
     }
  </code></pre>
  
  <p>Using the following test class</p>
  
  <pre><code>  @Test
    public void testdoLoginNegative() {
              MainClass m = spy(new MainClass());
              Client mockClient = mock(Client.class);
              WebTarget target = mock(WebTarget.class);
  
              Response loginRsp = Response.status(500).build();
              doReturn(mockClient).when(m).getRestClient();
              when(mockClient.target(anyString()).path(anyString())).thenReturn(target);
              //NPE on next line.
              when(target.request().headers(any(MultivaluedMap.class)).post(any(Entity.class))).thenReturn(loginRsp);// this line throws a Null pointer exception. 
              Assert.assertFalse(m.doIusLogin(getMockHeaders()));
     }
  </code></pre>
  
  <p>However, my mock seems to show a null pointer exception as indicated in the source code. Any thoughts on what I could be doing wrong.. would be greatly appreciated. </p>