title: how to mock a URL connection
body: <p>Hi I have a method that takes an URL as an input and determines if it is reachable. 
  Heres the code for that:</p>
  
  <pre><code>public static boolean isUrlAccessible(final String urlToValidate) throws WAGNetworkException {
          URL url = null;
          HttpURLConnection huc = null;
          int responseCode = -1;
          try {
              url = new URL(urlToValidate);
              huc = (HttpURLConnection) url.openConnection();
              huc.setRequestMethod("HEAD");
              huc.connect();
              responseCode = huc.getResponseCode();
          } catch (final UnknownHostException e) {
              throw new WAGNetworkException(WAGConstants.INTERNET_CONNECTION_EXCEPTION);
          } catch (IOException e) {
              throw new WAGNetworkException(WAGConstants.INVALID_URL_EXCEPTION);
          } finally {
              if (huc != null) {
                  huc.disconnect();
              }
          }
          return responseCode == 200;
      }
  </code></pre>
  
  <p>I want to unit test the isUrlAccessible() method using <code>PowerMockito</code>. I feel that I will need to use <code>whenNew()</code> to mock the creation of <code>URL</code> and the when <code>url.openConnection()</code> is called, return another mock <code>HttpURLConnection</code> object. But I have am not sure how to implement this? Am I on the right track? Can anyone help me in implementing this?</p>