Android, WireMock: java.lang.NoClassDefFoundError:
Android, WireMock: java.lang.NoClassDefFoundError:
Android Studio 2.3.1,Android 7.0
我的buld.gradle
android {
编译SDK版本23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile "org.mockito:mockito-core:+"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile "org.mockito:mockito-android:2.7.21"
androidTestCompile "org.powermock:powermock-api-mockito:1.6.6"
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'
//WireMock
androidTestCompile("com.github.tomakehurst:wiremock:2.6.0") {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'org.ow2.asm', module: 'asm'
exclude group: 'org.json', module: 'json'
}
androidTestCompile('com.squareup.okhttp3:mockwebserver:3.5.0') {
exclude module: 'okhttp'
}
}
我使用 WireMock 进行的仪器化测试(在文件夹 "androidTest" 中):
public class WireMockApplicationTestCase2 {
@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
private Context androidTestContext;
private Context applicationContext;
@Before
public void setUp() throws Exception {
this.androidTestContext = InstrumentationRegistry.getContext();
this.applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext();
wireMockServer = new WireMockServer(8089);
wireMockServer.start();
}
@Test
public void testWiremockPlusOkHttp() throws IOException {
String serviceEndpoint = "http://127.0.0.1:" + 8089;
OkHttpClient okHttpClient = // some code fot http client;
String uri = "/hello-world";
Request request = new Request.Builder()
.url(serviceEndpoint + uri)
.build();
Response response = okHttpClient.newCall(request).execute();
String responseBodyStr = response.body().string();
assertEquals("hello world", responseBodyStr);
}
}
但是当我开始这个测试时(在 Android 7.0 上)我得到错误:
$ adb shell am instrument -w -r -e debug false -e class com.mycompany.android.transport.WireMockApplicationTestCase2#testWiremockPlusOkHttp com.mycompany.android.dev.test/android.support.test.runner.AndroidJUnitRunner
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/client/HttpClientBuilder;
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:48)
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:53)
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:124)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:71)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:108)
at com.mycompany.android.transport.WireMockApplicationTestCase2.setUp(WireMockApplicationTestCase2.java:46)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod.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.RunBefores.evaluate(RunBefores.java:24)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.impl.client.HttpClientBuilder" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.mycompany.android.dev.test-2/base.apk", zip file "/data/app/com.mycompany.android.dev-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.android.dev.test-2/lib/x86, /data/app/com.mycompany.android.dev-1/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
在您的 gradle 文件中,您可能需要包含 android 版本的 apache httpclient:
androidTestCompile group: 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
我找到了问题的解决方案:
1.If 你使用 Android 5.0-(例如 Android 4.3)然后你 必须 使用 Wiremock 版本 2.0 .8-beta
androidTestCompile 'com.github.tomakehurst:wiremock:2.0.8-beta'
2.If 你使用 Android 5.0+ 你可以使用任何 Wiremock 版本(例如 2.5.0)
androidTestCompile 'com.github.tomakehurst:wiremock:2.6.0'
P.S。也许这是一个 Multidex 问题。
Android Studio 2.3.1,Android 7.0
我的buld.gradle
android { 编译SDK版本23 buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile "org.mockito:mockito-core:+"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile "org.mockito:mockito-android:2.7.21"
androidTestCompile "org.powermock:powermock-api-mockito:1.6.6"
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'
//WireMock
androidTestCompile("com.github.tomakehurst:wiremock:2.6.0") {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'org.ow2.asm', module: 'asm'
exclude group: 'org.json', module: 'json'
}
androidTestCompile('com.squareup.okhttp3:mockwebserver:3.5.0') {
exclude module: 'okhttp'
}
}
我使用 WireMock 进行的仪器化测试(在文件夹 "androidTest" 中):
public class WireMockApplicationTestCase2 {
@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
private Context androidTestContext;
private Context applicationContext;
@Before
public void setUp() throws Exception {
this.androidTestContext = InstrumentationRegistry.getContext();
this.applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext();
wireMockServer = new WireMockServer(8089);
wireMockServer.start();
}
@Test
public void testWiremockPlusOkHttp() throws IOException {
String serviceEndpoint = "http://127.0.0.1:" + 8089;
OkHttpClient okHttpClient = // some code fot http client;
String uri = "/hello-world";
Request request = new Request.Builder()
.url(serviceEndpoint + uri)
.build();
Response response = okHttpClient.newCall(request).execute();
String responseBodyStr = response.body().string();
assertEquals("hello world", responseBodyStr);
}
}
但是当我开始这个测试时(在 Android 7.0 上)我得到错误:
$ adb shell am instrument -w -r -e debug false -e class com.mycompany.android.transport.WireMockApplicationTestCase2#testWiremockPlusOkHttp com.mycompany.android.dev.test/android.support.test.runner.AndroidJUnitRunner
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/client/HttpClientBuilder;
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:48)
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:53)
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:124)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:71)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:108)
at com.mycompany.android.transport.WireMockApplicationTestCase2.setUp(WireMockApplicationTestCase2.java:46)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod.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.RunBefores.evaluate(RunBefores.java:24)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.impl.client.HttpClientBuilder" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.mycompany.android.dev.test-2/base.apk", zip file "/data/app/com.mycompany.android.dev-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.android.dev.test-2/lib/x86, /data/app/com.mycompany.android.dev-1/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
在您的 gradle 文件中,您可能需要包含 android 版本的 apache httpclient:
androidTestCompile group: 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
我找到了问题的解决方案:
1.If 你使用 Android 5.0-(例如 Android 4.3)然后你 必须 使用 Wiremock 版本 2.0 .8-beta
androidTestCompile 'com.github.tomakehurst:wiremock:2.0.8-beta'
2.If 你使用 Android 5.0+ 你可以使用任何 Wiremock 版本(例如 2.5.0)
androidTestCompile 'com.github.tomakehurst:wiremock:2.6.0'
P.S。也许这是一个 Multidex 问题。