kotlinmockwebserver

MockWebServer Connection refused: no further information


Using MockWebServer with the following dependencies:

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

And the following test class:

class MockWebServerTest() {
    private val mockWebServer = MockWebServer()
    private val client = HttpClient.newBuilder().build()

    @BeforeClass
    fun setup() {
        mockWebServer.start(8081)
        mockWebServer.dispatcher = object : Dispatcher() {
            override fun dispatch(request: RecordedRequest): MockResponse {
                when(request.path){
                    ("success") ->{
                        return MockResponse()
                            .setResponseCode(200)
                            .setBody("success")}
                    ("error") ->{
                        return MockResponse()
                                .setResponseCode(500)
                                .setBody("error")}
                    else -> {
                        return MockResponse()
                                .setResponseCode(200)
                    }
                }
            }
        }
    }

    @AfterClass
    fun teardown() {
        mockWebServer.shutdown()
    }
    @Test
    fun givenMockServerRuns_itShouldRespond(){
        val request = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8081/success"))
                .setHeader("Content-Type", "text/html")
                .GET()
                .build()
        val response = client.send(request, HttpResponse.BodyHandlers.ofString())
        assertThat(response.statusCode()).isEqualTo(200)
        assertThat(response.body()).isEqualTo("success")
    }
}

I always get Connection refused: no further information. I found some answers already on stackoverflow as for example this answer but I don't now what this solution there means, I assume it is only relevant for android.


Solution

  • I was not able to make the answer given by Jongho Jeon working. The setup method actually did not execute at all.

    To get it working I finally did this:

    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class MockWebServerTest {
    
        private val client = HttpClient.newBuilder().build()
        private val mockWebServer = MockWebServer()
    
        @BeforeAll
        fun setup() {
            mockWebServer.start(8081)
            mockWebServer.dispatcher = object : Dispatcher() {
                override fun dispatch(request: RecordedRequest): MockResponse {
                    return when(request.path){
                        ("/success") ->{
                            MockResponse()
                                    .setResponseCode(200)
                                    .setBody("success")
                        }
                        ("/error") ->{
                            MockResponse()
                                    .setResponseCode(500)
                                    .setBody("error")
                        }
                        else -> {
                            MockResponse()
                                    .setResponseCode(200)
                        }
                    }
                }
            }
        }
    
        @AfterAll
        fun teardown() {
            mockWebServer.shutdown()
        }
    
        @Test
        fun givenMockServerRuns_itShouldRespond(){
            val request = HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:8081/success"))
                    .setHeader("Content-Type", "text/html")
                    .GET()
                    .build()
            val response = client.send(request, HttpResponse.BodyHandlers.ofString())
            assertThat(response.statusCode()).isEqualTo(200)
            assertThat(response.body()).isEqualTo("success")
        }