vijayjcp / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 2 forks source link

mockin org.apache.http.impl.client.DefaultHttpClient class #507

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. create a constructor mock of .DefaultHttpClient using Powermock_V1.5.5 
Mockito_V1.9.5 and Junit_V4.1 ektorp_V1.4.1
2.run the test call new 
StdHttpClient.Builder().url(dburl.toString()).caching(true)          
.cleanupIdleConnections(true).socketTimeout(Integer.parseInt(socketTimeout)).bui
ld()

What is the expected output? What do you see instead?
1- connection established using mocked HttpClient
2- SSLContext.getInstance("TLS") throws NoSuchAlgorithmException

What version of the product are you using? On what operating system?
OS Ubuntu 14.04

Please provide any additional information below.

Test code 

@RunWith(PowerMockRunner.class)
@PrepareForTest(DefaultHttpClient.class)
public class App_UnitTest {

  protected static InputStream mockUrlConnectionInputStream;
  protected static OutputStream mockUrlConnectionOutputStream;

  @InjectMocks
  private final App appUnderTest = new App();
  @Spy
  private final CouchConnection couchConnection = new CouchConnection();

  @Before
  public void setupOnce() throws Exception {
    MockitoAnnotations.initMocks(this);
    couchConnection.setDbUrl("https://test.couch.url.com");
    couchConnection.setDbName("testCouchDB");
    couchConnection.setPort("12211");
    couchConnection.setSocketTimeout("10000");

    final DefaultHttpClient mockDefaultHttpClient = PowerMockito.mock(DefaultHttpClient.class);
    PowerMockito.whenNew(DefaultHttpClient.class).withAnyArguments().thenReturn(mockDefaultHttpClient);

    try {
      URL.setURLStreamHandlerFactory(new MockURLStreamHandler());
    } catch (final Error e) {
      // Do nothing, this will be called by all the extending test class but should only be called once for JVM so just
      // ignore
    }
  }

  @Test
  public void testifJobExists() throws IOException {
      final HttpClient authenticatedHttpClient = new StdHttpClient.Builder().url(dburl.toString()).caching(true)        .cleanupIdleConnections(true).socketTimeout(Integer.parseInt(socketTimeout)).build();

      final CouchDbInstance couchInstance = new StdCouchDbInstance(authenticatedHttpClient);

      couchConnector = couchInstance.createConnector(dbName, true);
  }

  public class MockURLStreamHandler extends URLStreamHandler implements URLStreamHandlerFactory {

    // *** URLStreamHandler
    @Override
    protected URLConnection openConnection(final URL u) throws IOException {
      return new MockHttpsURLConnection(u);
    }

    // *** URLStreamHandlerFactory
    @Override
    public URLStreamHandler createURLStreamHandler(final String protocol) {
      // this will only override the chosen protocol so that file and other protocols used by the Spring and other
      // classes will work as usual.
      // WARNING this will only mock https connections, so your URL has to start with https e.g.
      // https:\\something.etc.com
      if (protocol.equalsIgnoreCase("https")) {
        return this;
      } else {
        return null;
      }
    }
  }

  public class MockHttpsURLConnection extends HttpsURLConnection {

    protected MockHttpsURLConnection(final URL url) {
      super(url);
    }

    // *** HttpURLConnection
    @Override
    public InputStream getInputStream() throws IOException {
      return mockUrlConnectionInputStream;
    }

    @Override
    public void connect() throws IOException {
    }

    @Override
    public void disconnect() {
    }

    @Override
    public boolean usingProxy() {
      return false;
    }

    @Override
    public String getCipherSuite() {
      return null;
    }

    @Override
    public Certificate[] getLocalCertificates() {
      return null;
    }

    @Override
    public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
      return null;
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
      return mockUrlConnectionOutputStream;
    }

  }

}

Original issue reported on code.google.com by daghana...@gmail.com on 22 Jul 2014 at 4:55