title: Android Tests: Stubbing out Retrofit with Mockito
body: <p>In my <code>MainActivity</code> I have a method called <code>getAPI</code> that returns an <code>OTBServiceWrapper</code>. This is used to setup retrofit for calling to an API.</p>
  
  <p>In my <code>MainActivityTest</code> file I am trying to stub out the <code>new OTBService().getService()</code> call that the <code>getApi</code> method is making so I can return a <code>MockedOTBService</code> which changes the client to a custom one that return <code>json</code>.</p>
  
  <p>As is, the current implementation will it the <code>MockedOTBService</code> if I had to place a logger within <code>MockedOTBService</code> but also falls through and calls the real api, which is not want I want in a test.</p>
  
  <p>I am trying to stub the Retrofit API calls using Mockito and return <code>json</code>. I cant seem to understand why the stub is being called yet is not stubbing the method in question.</p>
  
  <p><strong>Notes:</strong></p>
  
  <ol>
  <li>I am using <code>ActivityInstrumentationTestCase2</code></li>
  <li>I am only running one test</li>
  <li>If I add a <code>verify(mockedOTBService, atLeastOnce()).getService();</code> is says it was never called.</li>
  <li>If I change the <code>when...thenReturn</code> to use a <code>mMainActivity = spy(getActivity())</code> there is not change and the real API is called.</li>
  </ol>
  
  <p><strong>Logcat Output</strong></p>
  
  <pre><code>Logger﹕ MockedOTBService was called // Mock is called
  Logger﹕ Real OTBService was called // Real API is called
  Logger﹕ MainActivity getAPI method class is "$Proxy1" // Mock is shown in MainActivity
  Logger﹕ RealAPIResponse JSON Parsed ID: 266 // Real API response returned
  </code></pre>
  
  <p><strong>Real Flow</strong></p>
  
  <p>MainActivity.onCreate() > OTBService.getService() > OTBServiceWrapper.createSearch(...)</p>
  
  <p><strong>Trying to Achieve within Tests</strong></p>
  
  <p>MainActivity.onCreate() > MockedOTBService.getService() > OTBServiceWrapper.createSearch(...)</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends Activity {
    private OTBServiceWrapper serviceWrapper;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      getApi().createSearch(...)
    }
  
    public OTBServiceWrapper getApi() {
      return new OTBService().getService();
    }
  }
  </code></pre>
  
  <p>OTBService.java</p>
  
  <pre><code>public class OTBService {
    public OTBServiceWrapper getService() {
      RestAdapter restAdapter = new RestAdapter.Builder()
          .setEndpoint(Constants.API_URL)
          .build();
  
      return restAdapter.create(OTBServiceWrapper.class);
    }
  }
  </code></pre>
  
  <p>OTBServiceWrapper.java</p>
  
  <pre><code>public interface OTBServiceWrapper {
    @POST(Constants.API_SEARCHES_POST_URL)
    void createSearch(@Body Request request, Callback&lt;Request.Response&gt; callback);
  }
  </code></pre>
  
  <p>MainActivityTest.java    </p>
  
  <pre><code>public class MainActivityTest extends ActivityInstrumentationTestCase2&lt;MainActivity&gt; {
    private OTBService mMockedOTBService;
    private MainActivity mMainActivity;
    private View mSearchButton;
  
    public MainActivityTest() { super(MainActivity.class); }
  
    @Override
    protected void setUp() throws Exception {
      super.setUp();
  
      setActivityInitialTouchMode(true);
  
      System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
  
      mMockedOTBService = mock(OTBService.class);
      when(mMockedOTBService.getService()).thenReturn(new MockedOTBService(getInstrumentation().getContext()).getService());
  
      mMainActivity = getActivity();
  
      mSearchButton = mMainActivity.findViewById(R.id.AbSearchButton);
      mYourHolidayButton = mMainActivity.findViewById(R.id.AbYourHolidayButton);
    }
  
    public void testButtonActions() {
      TouchUtils.clickView(this, mSearchButton);
      ...
    }
  }
  </code></pre>
  
  <p>MockedOTBService.java</p>
  
  <pre><code>public class MockedOTBService {
    private Context context;
  
    public MockedOTBService(Context context) { this.context = context; }
  
    public OTBServiceWrapper getService() {
      RestAdapter restAdapter;
  
      restAdapter = new RestAdapter.Builder()
          .setClient(new LocalJsonClient(context))
          .setEndpoint(Constants.API_TEST_URL)
          .build();
  
      return restAdapter.create(OTBServiceWrapper.class);
    }
  }
  </code></pre>
  
  <p>LocalJsonClient.java</p>
  
  <pre><code>@SuppressLint("DefaultLocale")
  public class LocalJsonClient implements Client { ... }
  </code></pre>
  
  <p>build.gradle</p>
  
  <pre><code>dependencies {
      androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
      androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
  }
  </code></pre>