Unable to test calls for real server after mocking mockwebserver on Android?
<p>I am first running a mocked login test which returns status code 400 followed by a second test that logs in using real service call. The tests are below:</p>

<pre><code>@Test
public void mock() throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setResponseCode(400));
    server.start();
    NetworkServiceHelper.init(String.valueOf(server.url("/"))); // Change retrofit baseUrl to localhost

    loginWithoutWaiting(username, password);

    server.shutdown();
}

@Test
public void real() {
    NetworkServiceHelper.init(NetworkServiceHelper.DEFAULT_URL); // Change retrofit baseUrl to a real url
    loginWithoutWaiting(username, password);
}
</code></pre>

<p>The real service does not get called. The EditText error message says </p>

<blockquote>
  <p>Unable to authenticate: Failed to connect to localhost/127.0.0.1:50162</p>
</blockquote>

<p>Here is the NetworkServiceHelper.init() function that initializes Retrofit instance:</p>

<pre><code>public static synchronized void init(String serverUrl) {

   // Override the GSON date format.
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();

    // We want to make the network calls asynchronous, so use createWithScheduler()
    RxJavaCallAdapterFactory rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());

    // Finally, build the Retrofit client.
    retrofit = new Retrofit.Builder()
            .baseUrl(serverUrl)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(rxAdapter)
            .client(buildClient())
            .build();
}
</code></pre>