NoSuchMethodError when trying to instantiate MockWebServer
See original GitHub issueWhen 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:
- Created 4 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
No action for us to take on this.