title: Mocking JAX RS Client response
body: <p>I'm learning how to unit test. I have a method that is using a jax-rs client to call external service. I'm trying to mock a response received from jax-rs client but I'm getting NullPointerException. Could someone please help with the issue.</p>
  <p>My business class with the method I'm trying to unit test.</p>
  <pre><code>@Stateless
  public class SinglePaymentManager {
  
      private JAXRSClientWrapper httpClient;
      private Client client;
      public SinglePaymentManager() {
      }
  
      @Inject
      public SinglePaymentManager(JAXRSClientWrapper httpClient) {
          this.httpClient = httpClient;
          this.client = httpClient.createClient(true);
      }
  
      public BankResponse initiatePayment(PaymentContext paymentContext) {
          // Extracting data from parameters
  
          Invocation.Builder invocationBuilder = client.target(uri)
              .request()
              // more headers
              .header(&quot;content-type&quot;, MediaType.APPLICATION_JSON);
  
          String responseAsString;
          Response.Status responseStatus;
          try (Response response = invocationBuilder.post(Entity.json(payload))) {
              responseAsString = response.readEntity(String.class);
              responseStatus = response.getStatusInfo().toEnum();
          }
  
          return new BankResponse(responseStatus, responseAsString);
      }
  </code></pre>
  <p>My test class</p>
  <pre><code>
  public class SinglePaymentManagerTest {
  
      private final String responseAsString = //expected response;
      private final Response.Status responseStatus = Response.Status.CREATED;
  
      private final JAXRSClientWrapper mockHTTPClient = mock(JAXRSClientWrapper.class);
      private final Client mockClient = mock(Client.class);
      private final WebTarget mockWebTarget = mock(WebTarget.class);
      private final Invocation.Builder mockBuilder = mock(Invocation.Builder.class);
      private final Response mockResponse = mock(Response.class);
  
      @Test
      public void testSomeMethod() {
  
          when(mockHTTPClient.createClient(true)).thenReturn(mockClient);
          SinglePaymentManager manager = new SinglePaymentManager(mockHTTPClient);
  
          when(mockClient.target(anyString())).thenReturn(mockWebTarget);
  
          when(mockWebTarget.request()).thenReturn(mockBuilder);
  
          when(mockBuilder.header(&quot;accept&quot;, MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);
  
          when(mockBuilder.post(any(Entity.class))).thenReturn(mockResponse);
          when(mockResponse.readEntity(any(Class.class))).thenReturn(responseAsString);
          when(mockResponse.getStatusInfo().toEnum()).thenReturn(responseStatus); // this is where I get NullPointer
         
          //requestData and bank object creation is omitted for brevity        
          PaymentContext context = new PaymentContext(requestData, bank);
  
          BankResponse response = new BankResponse(responseStatus, responseAsString);
  
          BankResponse serviceResponse = manager.initiatePayment(context);
  
          Assertions.assertEquals(response, serviceResponse);
  
      }
  }
  </code></pre>
  <p>Dependecties from POM.xml</p>
  <pre><code>&lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
              &lt;version&gt;5.6.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
              &lt;version&gt;5.6.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;5.6.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
              &lt;version&gt;4.5.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-junit-jupiter&lt;/artifactId&gt;
              &lt;version&gt;4.5.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
              &lt;version&gt;8.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>