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 ?

- 50,076
- 60
- 203
- 339

- 159
- 9
1 Answers
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.

- 1,328
- 1
- 13
- 34
-
1You have also not to use okHttp cache or any other caching mechanism when you use mockwebserver, in my case this was the problem. – Enrico Bruno Del Zotto May 16 '17 at 08:11
-
@lupsyn publish it as your response then :) – PrzemekTom May 18 '17 at 06:41