title: How to test a method which requires an external rest call?
body: <p>I would like to unit test a method which logs in the user via an external access management infrastructure. I do not know how to mock the <code>Response response</code> object.</p>
  
  <p>This is my code what I intend to test:</p>
  
  <pre><code>public String login(final String username, final String password) {
      String baseUrl = getOpenAMApiBaseUrl();
      String path = "/openam/json/authenticate";
  
      // Response NEEDS TO BE MOCKED SOMEHOW
      Response response = jaxClient.target(baseUrl + path)
              .request(MediaType.APPLICATION_JSON_TYPE)
              .header(HttpHeaderField.OPENAM_USERNAME, username)
              .header(HttpHeaderField.OPENAM_PASSWORD, password)
              .post(Entity.json(null));
  
      int status = response.getStatus();
      String jsonResponse = response.readEntity(String.class);
  
      if (status == Response.Status.OK.getStatusCode()) {
          try (JsonReader jsonReader = Json.createReader(new StringReader(jsonResponse))) {
              JsonObject jsonObject = jsonReader.readObject();
              String token = jsonObject.getString("tokenId");
              LOGGER.info("{} have successfully logged in", username);
              return token;
          } catch (NullPointerException e) {
              LOGGER.info("login failed, invalid json response for {}/{}", username, password);
              return null;
          }
      }
  
      String message = "an error occurred during logging in, http status: {}, user: {}/{}";
      if (status == Response.Status.UNAUTHORIZED.getStatusCode()) {
          message = "invalid credentials, http status: {}, user: {}/{}";
      }
      LOGGER.info(message, status, username, password);
      return null;
  }
  </code></pre>