Search code examples
androidandroid-espressomockwebserver

java.io.IOException: unexpected end of stream on http://localhost:33339/


I am using retrofit and okhttp3.mockwebserver.MockWebServer for Android Test. I am getting this error while running a test.

Gradle:

 androidTestImplementation "com.squareup.okhttp3:mockwebserver:3.14.9"
def retrofit_version = "2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Android Test class has:

mockServer = MockWebServer()
mockServer.start()

//In some other class
Retrofit.Builder()
            .baseUrl(mockServer.url("/").toString())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
            .build()


mockServer.enqueue(
        MockResponse()
            .setResponseCode(200)
            .setBody(getJson(fileName)))

I don't know what is missing tried adding header but still getting same error. Please help.


Solution

  • Corrected sequence of calling the mock api before the progress bar is displayed and the error is gone.