Я хочу смоделировать вызовы https в моих тестах Robolectric, используя wiremock, но когда я запускаю тесты
Я предполагаю, что проблема связана с зависимостями, но я не знаю, как ее решить. Я пытался исключить httpclient из зависимости от wiremock и использовать вместо этого httpclient-android, но он не работает (я получаю java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
)
Я получаю исключение:
java.lang.ExceptionInInitializerError
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:958)
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:91)
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:59)
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:148)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:74)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:119)
at com.inaki.robolectric4.MainActivityTest.helloWorld(MainActivityTest.kt:21)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:154)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java)
at org.apache.http.impl.client.HttpClientBuilder.$$robo$$org_apache_http_impl_client_HttpClientBuilder$build(HttpClientBuilder.java:958)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java)
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:91)
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:59)
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:148)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:74)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:119)
at com.inaki.robolectric4.MainActivityTest.helloWorld(MainActivityTest.kt:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:151)
... 25 more
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.__staticInitializer__(SSLConnectionSocketFactory.java:146)
... 31 more
здесьэто тест:
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
@Test
fun helloWorld() {
val wireMockServer = WireMockServer()
wireMockServer.stubFor(get("/v1/test")
.willReturn(aResponse().withStatus(200)))
wireMockServer.start()
ActivityScenario.launch(LoginActivity::class.java)
onView(withId(R.id.hello)).check(matches(withText("Hello World!")))
}
}
вот зависимости:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:rules:1.2.0'
testImplementation 'androidx.test:runner:1.2.0'
testImplementation ("com.github.tomakehurst:wiremock:2.25.1")
testImplementation group: 'androidx.test.ext', name: 'junit', version: '1.1.1'
testImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation ("org.robolectric:robolectric:4.3.1")
}