使用WireMockRule运行Android UI测试时出现java.lang.NoClassDefFoundError

问题描述

我花了很多时间试图找出如何使用WireMock运行Android Espresso测试时解决此错误。

java.lang.NoClassDefFoundError:无法解决以下问题: Lorg / apache / http / protocol / HttpRequestExecutor

错误发生在以下代码行:

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8080));

我的build.grade

androidTestImplementation 'androidx.test:rules:1.1.0'

androidTestImplementation("com.github.tomakehurst:wiremock:2.18.0") {
    exclude group: 'org.apache.httpcomponents',module: 'httpclient'
    exclude group: 'asm',module: 'asm'
    exclude group: 'org.json',module: 'json'
    exclude group: 'com.google.guava',module:'guava'
}

androidTestImplementation("org.apache.httpcomponents:httpclient-android:4.3.5.1")

更多日志:

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/protocol/HttpRequestExecutor;
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:695)
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:76)
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:58)
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:131)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:73)
at com.github.tomakehurst.wiremock.junit.WireMockRule.<init>(WireMockRule.java:43)
at com.github.tomakehurst.wiremock.junit.WireMockRule.<init>(WireMockRule.java:39)

我该如何解决?

解决方法

我自己改用Wiremock独立版本进行了修复。

androidTestImplementation "com.github.tomakehurst:wiremock-standalone:2.26.3"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...