question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NoSuchMethodError when trying to instantiate MockWebServer

See original GitHub issue

When attempting to use Retrofit and MockWebServer in my project, I get the following exception when initializing a MockWebServer object in my unit tests:

java.lang.NoSuchMethodError: okhttp3.internal.Util.immutableListOf([Ljava/lang/Object;)Ljava/util/List;
	at okhttp3.mockwebserver.MockWebServer.<init>(MockWebServer.kt:176)

This bug can be reproduced in MockWebServerInitializationBugTest.java from https://github.com/jsoberg/OkHttp-MockWebServer-NoSuchMethorErrorBug.

The following is the gradle dependencies output for the example project:

compileClasspath - Compile classpath for source set 'main'.
\--- com.squareup.retrofit2:retrofit:2.7.0
     \--- com.squareup.okhttp3:okhttp:3.14.4
          \--- com.squareup.okio:okio:1.17.2

default - Configuration for default artifacts.
\--- com.squareup.retrofit2:retrofit:2.7.0
     \--- com.squareup.okhttp3:okhttp:3.14.4
          \--- com.squareup.okio:okio:1.17.2

implementation - Implementation only dependencies for source set 'main'. (n)
+--- unspecified (n)
\--- com.squareup.retrofit2:retrofit:2.7.0 (n)

runtimeClasspath - Runtime classpath of source set 'main'.
\--- com.squareup.retrofit2:retrofit:2.7.0
     \--- com.squareup.okhttp3:okhttp:3.14.4
          \--- com.squareup.okio:okio:1.17.2

testCompileClasspath - Compile classpath for source set 'test'.
+--- com.squareup.retrofit2:retrofit:2.7.0
|    \--- com.squareup.okhttp3:okhttp:3.14.4 -> 4.3.1
|         +--- com.squareup.okio:okio:2.4.1
|         |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.50 -> 1.3.61
|         |    |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.61
|         |    |    \--- org.jetbrains:annotations:13.0
|         |    \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50 -> 1.3.61
|         \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.61 (*)
+--- junit:junit:4.12
|    \--- org.hamcrest:hamcrest-core:1.3
+--- com.squareup.okhttp3:okhttp:4.3.1 (*)
\--- com.squareup.okhttp3:mockwebserver:4.3.1
     +--- com.squareup.okhttp3:okhttp:4.3.1 (*)
     \--- junit:junit:4.12 (*)

testImplementation - Implementation only dependencies for source set 'test'. (n)
+--- junit:junit:4.12 (n)
+--- com.squareup.okhttp3:okhttp:4.3.1 (n)
\--- com.squareup.okhttp3:mockwebserver:4.3.1 (n)

testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- com.squareup.retrofit2:retrofit:2.7.0
|    \--- com.squareup.okhttp3:okhttp:3.14.4 -> 4.3.1
|         +--- com.squareup.okio:okio:2.4.1
|         |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.50 -> 1.3.61
|         |    |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.61
|         |    |    \--- org.jetbrains:annotations:13.0
|         |    \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50 -> 1.3.61
|         \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.61 (*)
+--- junit:junit:4.12
|    \--- org.hamcrest:hamcrest-core:1.3
+--- com.squareup.okhttp3:okhttp:4.3.1 (*)
\--- com.squareup.okhttp3:mockwebserver:4.3.1
     +--- com.squareup.okhttp3:okhttp:4.3.1 (*)
     \--- junit:junit:4.12 (*)

Seemingly, the MockWebServer and OkHttp dependencies are both using the same version (4.3.1), and I would therefore think that there would be no issues.

Retrofit’s OkHttp dependency appears to be being updated to the same (from the line com.squareup.okhttp3:okhttp:3.14.4 -> 4.3.1). If the Retrofit dependency is removed, it works as expected.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
swankjessecommented, Mar 19, 2020

I believe this is an issue with conflicting dependency versions. If it only occurs in IntelliJ it’s probably an IntelliJ bug, and they’ve got a redesign for their project model this year that might fix it.

In the interim you can either upgrade your dependencies to the same version, or configure IntelliJ to build and run tests with Gradle. This is in the Gradle section in settings.

0reactions
swankjessecommented, Mar 19, 2020

No action for us to take on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MockWebServer: java.lang.NoSuchMethodError
Try adding this: testImplementation("com.squareup.okhttp3:mockwebserver:4.0.0") testImplementation("com.squareup.okhttp3:okhttp:4.0.0").
Read more >
void okhttp3.internal.internal.initializeinstancefortests()
I am new to Android Unit testing and I want to test if the API call is successful or not. Favicon for stackoverflow.com....
Read more >
zalando/logbook - Gitter
Found one issue while using Zalando logbook with okhttp3 java.lang.NoSuchMethodError: 'okhttp3.ResponseBody okhttp3.ResponseBody.create(byte[], okhttp3.
Read more >
ConnectException: Failed to connect to-kotlin
newInstance() @get:Rule val activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java) private val mockWebServer ...
Read more >
Integration Testing with MockWebServer
Using MockWebServer with WebClient for testing Spring WebFlux. ... If you want to skip to the GitHub repo with the build.gradle file and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found