【问题标题】:java.lang.NoSuchFieldError: INSTANCE in try to run WireMockServerjava.lang.NoSuchFieldError: 尝试运行 WireMockServer 中的实例
【发布时间】:2019-10-24 08:30:44
【问题描述】:

我想在我的 Robolectric 测试中使用 wiremock 模拟 https 调用,但是当我运行测试时

我猜这个问题是关于依赖的,但我不知道如何解决它。我试图从wiremock依赖中排除httpclient,并改用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")
}

【问题讨论】:

  • 我遇到了同样的问题。你找到解决办法了吗?如果是这样,你可以发布它吗?非常感谢!

标签: android robolectric wiremock


【解决方案1】:

似乎与 Robolectric 存在一些类路径冲突,我也无法确定。但是,由于过去 similar issues 使用 Wiremock,作者提供了相当长一段时间的 -standalone artifact,它内化了所有外部依赖项,并且可以与 Robolectric 一起使用(使用 4.5* 测试)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 2021-09-19
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    相关资源
    最近更新 更多