在 mockito 中创建 OkHttpClient 时如何解决 java.lang.AssertionError?
How to resolve java.lang.AssertionError when creating OkHttpClient in mockito?
我正在尝试制作一些罐装网络响应。我有实际请求的 json 响应,我有序列化响应的 Retrofit 接口。尝试设置它让我感到非常沮丧。我应该在这里做什么?看来我的选择是,1) 使用 MockWebServer() 2) 使用 RequestInterceptor()。
在尝试使用 1 或 2 时,我无法在没有失败的情况下实例化 OkHttpClient(),基本上这会使我尝试的所有东西立即死亡。我得到一个 java.lang.AssertionError 因为 OkHttpClient 在找不到 TLS 算法时抛出这个。
if (builder.sslSocketFactory != null || !isTLS) {
this.sslSocketFactory = builder.sslSocketFactory;
} else {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
this.sslSocketFactory = sslContext.getSocketFactory();
} catch (GeneralSecurityException e) {
**throw new AssertionError(); // The system has no TLS. Just give up.**
}
}
我尝试使用 unMock 将 "javax.net.ssl" class 保留在 android.jar 中,但这并没有解决错误。
unMock {
// URI to download the android-all.jar from. e.g. https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/
downloadFrom 'https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar'
keep "android.util.Log"
keep "javax.net.ssl"
}
所以基本上,我遇到过各种如何使用改造 2 模拟网络请求的示例,但我无法越过这个障碍,我感到很失败。我还没有看到其他人遇到过这个问题,我对每个人如何在他们的所有测试中轻松实例化新的 OkHttpClients 感到困惑。
下面是我使用的相关依赖
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.powermock:powermock-mockito-release-full:1.6.4'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.easymock:easymock:3.4'
testCompile 'org.powermock:powermock-api-easymock:1.6.4'
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
compile 'com.google.code.gson:gson:2.4'
您需要在 class 顶部添加此注释。当然可以。
@PowerMockIgnore("javax.net.ssl.*")
我正在尝试制作一些罐装网络响应。我有实际请求的 json 响应,我有序列化响应的 Retrofit 接口。尝试设置它让我感到非常沮丧。我应该在这里做什么?看来我的选择是,1) 使用 MockWebServer() 2) 使用 RequestInterceptor()。
在尝试使用 1 或 2 时,我无法在没有失败的情况下实例化 OkHttpClient(),基本上这会使我尝试的所有东西立即死亡。我得到一个 java.lang.AssertionError 因为 OkHttpClient 在找不到 TLS 算法时抛出这个。
if (builder.sslSocketFactory != null || !isTLS) {
this.sslSocketFactory = builder.sslSocketFactory;
} else {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
this.sslSocketFactory = sslContext.getSocketFactory();
} catch (GeneralSecurityException e) {
**throw new AssertionError(); // The system has no TLS. Just give up.**
}
}
我尝试使用 unMock 将 "javax.net.ssl" class 保留在 android.jar 中,但这并没有解决错误。
unMock {
// URI to download the android-all.jar from. e.g. https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/
downloadFrom 'https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar'
keep "android.util.Log"
keep "javax.net.ssl"
}
所以基本上,我遇到过各种如何使用改造 2 模拟网络请求的示例,但我无法越过这个障碍,我感到很失败。我还没有看到其他人遇到过这个问题,我对每个人如何在他们的所有测试中轻松实例化新的 OkHttpClients 感到困惑。
下面是我使用的相关依赖
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.powermock:powermock-mockito-release-full:1.6.4'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.easymock:easymock:3.4'
testCompile 'org.powermock:powermock-api-easymock:1.6.4'
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
compile 'com.google.code.gson:gson:2.4'
您需要在 class 顶部添加此注释。当然可以。
@PowerMockIgnore("javax.net.ssl.*")