0

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.

Sjoerd222888
  • 3,228
  • 3
  • 31
  • 64

2 Answers2

0

I think your server is not started because the setup() method is not called. Please make sure that the setup() method is called before the test. You are using the JUnit 4 framework on the Kotlin/JVM. In Java, the method annotated with @BeforeClass must be static. But you're using Kotlin/JVM so I think you need to change your code like this using companion object and @JvmStatic:

class MockWebServerTest() {
    // ...
    companion object {
        @BeforeClass
        @JvmStatic
        fun setup() {
            // ...
        }
        // teardown method also should be here
    }
    // your test

You can check if the server is started or not in the beginning of the test by some methods such as mockserver.started() (I'm not sure what is the exact method name)

@Test
fun test() {
    assert mockserver.started()
    // your test code
}
Jongho Jeon
  • 96
  • 1
  • 3
0

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")
    }
Sjoerd222888
  • 3,228
  • 3
  • 31
  • 64