kotlin/TypeCastException when trying to create OkHttpClient object

Did you add the kotlin stdlib and stdlib-common dependencies to your build? Looks like they're absent.


I had the same problem. Add kotlin-stdlib JAR to the build Path


The solution that worked for me (which is mentioned in this github comment) is to add okhttp library itself:

Gradle kotlin DSL:

testImplementation("com.squareup.okhttp3:okhttp:4.2.2")
testImplementation("com.squareup.okhttp3:mockwebserver:4.2.2")

Gradle Groovy DSL:

testImplementation 'com.squareup.okhttp3:okhttp:4.2.2'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.2.2'

It is likely that your project includes the version of okhttp as version 3.x.x, causing the mockwebserver to depend on the wrong version of okhttp which also does not yet contains the transitive dependency on kotlin-stdlib-common.

If you are using Spring Boot along with mockwebserver just add the following property to your pom

<okhttp3.version>4.x.x</okhttp3.version>

Tags:

Java

Okhttp

Okio