title: I got javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated on my JUnit test
body: <p>I am creating some unit tests for an app that uses a REST API. When I try to send a HttpPost request to the server URL (https://some.server.com), I got this:</p>
  
  <pre><code>javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
      at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
      at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
      at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:399)
      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
      at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
      at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
  </code></pre>
  
  <p>The server has a verified HTTPS certificate. Also, when I run this on production, it works fine. So I think it has to do with JUnit and/or my local computer.</p>
  
  <p>I am also using a HttpClient created with this:</p>
  
  <pre><code>public static class WebClientDevWrapper {
      public static HttpClient wrapClient(HttpClient base) {
          try {
              SSLContext ctx = SSLContext.getInstance("TLS");
              X509TrustManager tm = new X509TrustManager() {
  
                  public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                  }
  
                  public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                  }
  
                  public X509Certificate[] getAcceptedIssuers() {
                      return null;
                  }
              };
              ctx.init(null, new TrustManager[]{tm}, null);
              SSLSocketFactory ssf = new SSLSocketFactory(ctx);
              ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
              ClientConnectionManager ccm = base.getConnectionManager();
              SchemeRegistry sr = ccm.getSchemeRegistry();
              sr.register(new Scheme("https", ssf, 443));
              return new DefaultHttpClient(ccm, base.getParams());
          } catch (Exception ex) {
              ex.printStackTrace();
              return null;
          }
      }
  }
  </code></pre>
  
  <p>AFAIK, if I create a HTTPClient with the code above, it should avoid <code>SSLPeerUnverifiedException</code>, but it seems it is not working.</p>
  
  <p>So how can I solve this problem? I have tried more ways to create HttpClients which don't complain about SSL certs, but nothing has worked so far.</p>