title: test jersey 2 client and webtarget
body: <p>I am using jersey 2</p>
  
  <p>I have an abstract class with which I construct my request.
  Now, I also have some abstract client classes I use as proxy classes and actual implementations. These work well, but are untested. </p>
  
  <p>My question is how I could test this, without having to run the webservice which it is connecting to?</p>
  
  <pre><code>public abstract class AbstractRestProxy {
  
  private Client client;
  private WebTarget service;
  
  /**
   * Get the base {@link Client} and {@link WebTarget}
   */
  @PostConstruct
  public void base() {
      this.client = ClientBuilder.newClient();
      this.service = this.client.target(this.getBaseUri());
  }
  
  /**
   * close the connection before destroy
   */
  @PreDestroy
  protected void close() {
      if (this.client != null) {
          this.client.close();
      }
  }
  
  /**
   *
   * @return get the basePath
   */
  protected abstract String getBasePath();
  
  /**
   *
   * @return get the baseUri
   */
  protected abstract String getBaseUri();
  
  /**
   *
   * @param paths
   *            the paths to get for the rest service
   * @return {@link javax.ws.rs.client.Invocation.Builder}
   */
  protected Builder getRequest(final String... paths) {
      WebTarget serviceWithPath = this.getServiceWithPaths();
      for (final String path : paths) {
          serviceWithPath = serviceWithPath.path(path);
      }
      return serviceWithPath.request(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON);
  }
  </code></pre>
  
  <p>A method I use to, for example get a response with an identifier, I use this method.</p>
  
  <pre><code>public Response getByID(final ID identifier) {
      return this.getRequest(identifier.toString()).get();
  }
  </code></pre>