使用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"