title: Testing Instrumental (Espresso) Login using MockWebServer with Retrofit
body: <p>I'm looking to create an instrumental (espresso) test from LoginActivy.
  I'm using Mock's for API calls, unfortunately my app keeps calling my REAL API.</p>
  
  <p>Below is my test code</p>
  
  <pre><code>@Test
  public void loginSuccessTest() throws Exception {
      String fileName = "login/success_login_with_user_pass.json";
      try {
          Thread.sleep(16);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
      onView(withId(R.id.txt_username)).perform(replaceText(USER_VALID));
  
      onView(withId(R.id.txt_password)).perform(replaceText(PASS_VALID), closeSoftKeyboard());
  
      server.enqueue(
              new MockResponse()
                      .setResponseCode(200)
                      .setBody(RestServiceTestHelper.getStringFromFile(getInstrumentation().getContext(), fileName)));
  
  
      ViewInteraction appCompatButton = onView(
              allOf(withId(R.id.btn_login), withText("Acessar")));
      appCompatButton.perform(scrollTo(), click());
  
      onView(withId(R.id.btn_main_menu)).check(matches(isDisplayed()));
  }
  </code></pre>
  
  <p>When the test passes the code below, the test calls the REAL API.</p>
  
  <pre><code>ViewInteraction appCompatButton = onView(
              allOf(withId(R.id.btn_login), withText("Acessar")));
      appCompatButton.perform(scrollTo(), click());
  </code></pre>
  
  <p>This is the code before the test starts</p>
  
  <pre><code>@Before
  public void setUp() throws Exception {
      server = new MockWebServer();
      server.start(8681);
  }
  </code></pre>
  
  <p>The application is using the productFlavors configuration:</p>
  
  <pre><code>productFlavors {
      ui_tests {
          buildConfigField STRING, 'HOST', '"http://localhost:8681/"'
      }
  }
  </code></pre>
  
  <p>I do not know what else to do to avoid calls in my REAL API, can anyone help me?</p>
  
  <p>Configuring the Android Studio Variable Environment
  <a href="https://i.stack.imgur.com/7Zr9N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Zr9N.png" alt="Build Variants"></a></p>