Search code examples
spockokhttpmockwebserver

MockWebServer: java.lang.NoSuchMethodError


Trying MockWebServer for the first time on a Groovy/Spring project that uses Spock for unit testing.

I added MockWebServer dependencies as directed (I had to add the second line myself to avoid errors, but it's not documented:

testImplementation("com.squareup.okhttp3:mockwebserver:4.0.0")
testImplementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.40")

I have a basic Spock test that looks like this:

def 'server'() {
    setup:
    MockWebServer server = new MockWebServer()

    expect:
    server
}

But it fails with this output:

java.lang.NoSuchMethodError: okhttp3.internal.Util.immutableListOf([Ljava/lang/Object;)Ljava/util/List;

    at okhttp3.mockwebserver.MockWebServer.<init>(MockWebServer.kt:176)

Is there another dependency I'm missing? Does MockWebServer not play well with Groovy and Spock?

For what it's worth, using version 3.1.4 seems to work:

testImplementation("com.squareup.okhttp3:mockwebserver:3.14.2")

(I'm a first time user of MockWebServer)

Thank you!


Solution

  • Try adding this:

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

    With MockWebServer your OkHttp dependency must be the same version.