I want to use MockWebServer
to mock the response of requests in my UI tests.
Every getting-started-guide or tutorial I found place the url in a custom MyApp extends Application
class and make it available by public String getApiUrl {return "http://realUrl";}
. In tests they create an MyTestApp extends MyApp
and override @Override getApiUrl() {return "http://testurl";}
.
The problem is that I define the url in a repository where I have no access to Application
, so I cannot call getApplication.getApiUrl()
. For this reason, I am looking for an alternative without extending Application
.
One option that comes to my mind is to implement a function setUrl(String url)
in the repository. Then I implement a class TestApplication extends Application
and override onCreate()
and call repository.setUrl("http://testurl")
. However this bring testcode into my production code, which I would like to avoid.
My favorite solution would be to mock the repository or a simple constants class, so I can tell that class to simply return another url in tests.