In my MockWebServer
I'm using two different dispatcher. One LocalResponseDispatcher
for locally add response and a normal QueueDispatcher
in order to enqueue some stub responses. I had problem to switch between twos. Infact when I'm trying to use .setDispatcher()
the webserver answer always with the response of the previous one. Have you got same issues ?
I had similar issue. Every test worked separately, but for more than one only the first did his job. I found out, that my problem lied in different place. When you use MockWebServer remember, that if you execute in @Before
method
mockWebServer.url()
the port for your API is almost always different for every test. Port number in the url changes, eg. from http://localhost:58919/
to http://localhost:52915/
My problem was that I instantiated my APIs with url only once. It is usually good idea, because your API url doesn't change during your app work. Solution is to provide setBaseUrl(String url)
to TestApplication extends Application
(with appropriate custom jUnit runner) which recreate your API in the end: in my example, I reinstantiated Retrofit and API - tests started working like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With