Mockwebserver takeRequest() method infinite execution

1.1k views Asked by At

I added a request using enque (), but using the debugger, you can see that when you call takeRequest inside it, requestQueue = 0, which means that there is no request, after which takeRequest () waits for a request indefinitely. What could be the problem?

class ExampleUnitTest {
    private lateinit var mockWebServer: MockWebServer

    @Before
    fun setup() {
        mockWebServer = MockWebServer()
        mockWebServer.start()
        mockWebServer.url("/")
    }

    @After
    fun teardown() {
        mockWebServer.shutdown()
    }

    @Test
    fun `test`(){
        val response = MockResponse()
            .setResponseCode(HttpURLConnection.HTTP_OK)
            .setBody("response body")
        mockWebServer.enqueue(response)
        var recordedRequest = mockWebServer.takeRequest()

        ...
    }
}
1

There are 1 answers

0
laalto On BEST ANSWER

There are no requests since you never requested anything. You need to actually call some code that fires a request with the base URL as returned by mockWebServer.url("/").