WireMock, Android: java.lang.NoClassDefFoundError

412 views
Skip to first unread message

Alexei J

unread,
May 1, 2017, 7:56:47 AM5/1/17
to wiremock-user
Android Studio 2.3.1, Android 7.0

My buld.gradle

android {
    compileSdkVersion 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:+"

    // for folder "androidTest"
    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'
    }
}

My instrumented test (in folder "androidTest")  with WireMock:

public class WireMockApplicationTestCase2 {

    Logger logger = LoggerFactory.getLogger(WireMockApplicationTestCase2.class);
    WireMockServer wireMockServer;


    @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 {
        logger.debug("testWiremockPlusOkHttp");

        String serviceEndpoint = "http://127.0.0.1:" + 8089;
        logger.debug("WireMock Endpoint: " + serviceEndpoint);

        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();

        logger.debug("Response Body: " + responseBodyStr);
        assertEquals("hello world", responseBodyStr);
    }
}




But when I start this test (Android 7.0) I get error:


Testing started at 14:23 ...

05/01 14:23:10: Launching testWiremockPlusOk...()
$ adb push D:\myProjejt\app\build\outputs\apk\app-dev_no_verification.apk /data/local/tmp/com.mycompany.android.dev
$ adb shell pm install -r "/data/local/tmp/com.mycompany.android.dev"
Success


No apk changes detected since last installation, skipping installation of D:\myproject\app\build\outputs\apk\app-dev-androidTest.apk
$ adb shell am force-stop com.mycompany.android.dev.test
Running tests

$ 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
Client not ready yet..
Started running tests

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$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.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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)
... 36 more

Tests ran to completion.













Alexei J

unread,
May 20, 2017, 4:54:55 AM5/20/17
to wiremock-user
I found solutions of problem:

If you use Android 5.0- (e.g. Android 4.3) then you MUST use Wiremock version 2.0.8-beta

androidTestCompile 'com.github.tomakehurst:wiremock:2.0.8-beta'


If you use Android 5.0+ you can you use any Wiremock version (e.g. 2.5.0)

androidTestCompile 'com.github.tomakehurst:wiremock:2.6.0'


P.S. Maybe this is a Multidex issues.

понедельник, 1 мая 2017 г., 14:56:47 UTC+3 пользователь Alexei J написал:
Reply all
Reply to author
Forward
0 new messages