okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

MockWebServer clearing all requests at once

Open BarracudaX opened this issue 4 years ago • 2 comments

I am using MockWebServer as a bean in Spring Boot Test. The problem I have is that not all test methods need to get the request sent, but when I get to the test method that needs to get request sent, it first gets requests sent in previously executed test methods. Maybe a method on MockWebServer that clears all requests could help in situations like this.

Currently a work-around I am using is a kotlin extension method that calls takeRequest() n times, where n = requestCount of MockWebServer.

Maybe the same functionality could be useful for enqueued responses to set default test context. For example, setting mock web server to enqueue a successful response and then, for other cases, clear the enqueued response(-s) and enqueue a different response.

BarracudaX avatar Dec 20 '21 09:12 BarracudaX

Actually, just found out, my work-around does not work because I assumed requestCount returns a number of requests sent but not taken with takeRequest, but it returns the total number of requests sent including those sent and taken with takeRequest. Calling the time-based takeRequest should work.

BarracudaX avatar Dec 20 '21 10:12 BarracudaX

Can you use different mock webserver instances?

See the description in https://github.com/square/okhttp/issues/6906#issuecomment-966858483

There is a deliberate design choice to make MockWebServer single use. If you look at MockWebServerRule (junit4) or MockWebServerExtension (junit5) you will see that is how they are implemented.

yschimke avatar Dec 26 '21 09:12 yschimke

Closing as low priority, unlikely to get addressed.

yschimke avatar May 21 '23 09:05 yschimke