I am trying to use okhttp3.mockwebserver.MockWebServer for some intrumented testing on Android. This is what I have in my build.gradle (:app) file for instrumented testing:
androidTestImplementation 'androidx.annotation:annotation:1.1.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestUtil 'androidx.test:orchestrator:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test:core:1.3.0'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0'
Here's my java file in andoidTest/java folder:
import android.content.Intent;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
public class MainActivityTest {
MockWebServer server = new MockWebServer();
@Rule
public ActivityTestRule<MainActivity> activityRule
= new ActivityTestRule<>(MainActivity.class);
@Test
public void testNoSessionData() {
try {
server.enqueue(new MockResponse().setBody("[]").setResponseCode(200));
server.start(8080);
activityRule.launchActivity(
new Intent(
InstrumentationRegistry.getInstrumentation().getContext(),
MainActivity.class
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
} catch (Exception e) {
ExceptionTracker.log(e);
}
}
and here's the exception I'm seeing:
java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/concurrent/TaskRunner$RealBackend;
at okhttp3.mockwebserver.MockWebServer.<init>(MockWebServer.kt:101)
at and.dev.cell.MainActivityTest.<init>(MainActivityTest.java:18)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: java.lang.ClassNotFoundException: Didn't find class "okhttp3.internal.concurrent.TaskRunner$RealBackend" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/and.dev.cell.test-BTg7mf7fGT0rZNPUutH4Kg==/base.apk", zip file "/data/app/and.dev.cell-zdMzg_u88rFRy9yIIP8HqQ==/base.apk"],nativeLibraryDirectories=[/data/app/and.dev.cell.test-BTg7mf7fGT0rZNPUutH4Kg==/lib/x86, /data/app/and.dev.cell-zdMzg_u88rFRy9yIIP8HqQ==/lib/x86, /data/app/and.dev.cell.test-BTg7mf7fGT0rZNPUutH4Kg==/base.apk!/lib/x86, /data/app/and.dev.cell-zdMzg_u88rFRy9yIIP8HqQ==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 29 more
Verify that you have the same version of okhttp for all your libraries, if you are not sure you can force it with the next code:
or adding explicitly in your application gradle file.