4

我创建了一个HttpsURLConnection基于StackExchange 答案的模拟:

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

...

@RunWith(PowerMockRunner.class)
public class DialogTest {
    public void mockHttpsUrlConnectionExample() throws Exception
    {
        URL mockUrl = PowerMockito.mock(URL.class);
        PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockUrl);
        HttpsURLConnection mockUrlConnection = PowerMockito.mock(HttpsURLConnection.class);
        PowerMockito.when(mockUrl.openConnection()).thenReturn(mockUrlConnection);
        PowerMockito.when(mockUrlConnection.getResponseCode()).thenReturn(200);

        // Create and call my objects ...

    }
}

但是,当我使用它时,我看到了一个强制转换异常:

java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

问题在于这段代码:

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

...

private Boolean sendRequest(String endpoint, JSONObject requestData, Boolean throwOnAuthException) throws JSONException, IOException {
    this.responseData = null;

    try {
        String serviceURI = getServiceURI();
        String dialogUri = String.format("%s%s", serviceURI, endpoint);
        URL url = new URL(dialogUri);

        // Exception source is this cast
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

但是,当我查看源代码时,我看到sun.net.www.protocol.https.HttpsURLConnectionImpl实现javax.net.ssl.HttpsURLConnection

有关如何解决此问题的任何建议?

4

1 回答 1

6

问题是常规类加载器和 PowerMock 之间的冲突

PowerMock 的缺点是使用了自定义类加载器。此类加载器可以以与默认类加载器不兼容的方式修改类型签名。

在某些情况下,通过反射进行实例化将导致使用默认类加载器来加载类型。由于使用了不同的签名,该类加载器不会知道 PowerMock 已经加载了一个类型。结果可能是应该实现强制转换类型的对象的强制转换错误。

为避免此问题,请先停止 PowerMock 加载javax.net.ssl.HttpsURLConnection

要防止强制转换异常,请确保 javax.net.ssl.HttpsURLConnection 仅由一个类加载器加载。由于我无法阻止使用常规类加载器,因此最好的方法是阻止 PowerMock 加载器使用@PowerMockIgnore注释进行操作。例如

@PowerMockIgnore({"javax.net.ssl.*"})
@PrepareForTest(android.util.Log.class)
public class DialogTest {
...

副作用是 PowerMock 不再能够提供它的版本HttpsURLConnection

接下来,暴露 HttpsURLConnection 构造,并替换一个模拟对象

为 HttpsURLConnection 引入一个工厂。例如

public class HttpsUrlConnectionProvider {
    public HttpsURLConnection getHttpsURLConnection(String dialogUri) throws IOException {
        URL url = new URL(dialogUri);
        return (HttpsURLConnection) url.openConnection();
    }
}

创建用于 HTTP 请求的 HttpsURLConnection 对象的模拟,例如

final HttpsURLConnection mockUrlConnection = PowerMockito.mock(HttpsURLConnection.class);
PowerMockito.when(mockUrlConnection, "getResponseCode").thenReturn(200);
PowerMockito.when(mockUrlConnection, "getOutputStream").thenReturn(outputStream);

// Replace the HttpsURLConnection factory with one that returns our mock HttpsURLConnection
HttpsUrlConnectionProvider mockConnFactory = new HttpsUrlConnectionProvider() {
    public HttpsURLConnection getHttpsURLConnection(String dialogUri) throws
            IOException {
       return mockUrlConnection;
    }
};
dialog.setHttpsUrlConnectionProvider(mockConnFactory);
于 2015-07-02T11:27:00.843 回答