Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MockWebServer dispatcher problems

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 ?

like image 295
Enrico Bruno Del Zotto Avatar asked Oct 30 '22 10:10

Enrico Bruno Del Zotto


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.

like image 96
Przemo Avatar answered Nov 11 '22 10:11

Przemo