title: MockWebserver and Retrofit 2.0
body: <p>Trying to unit test a <code>Retrofit 2.0</code> response using <code>MockWebServer</code>. I have the webserver setup, but the problem occurs when i am trying to pass a mock <code>json</code> file as a response. To illustrate, my folder structure is:</p>
  
  <pre><code>src-&gt;test-&gt;java-&gt;package_name-&gt;class_name.java
  
  src-&gt;test-&gt;resources-&gt;list_success.json
  </code></pre>
  
  <p>My unit test:</p>
  
  <pre><code>@Before
  public void setUp() throws Exception{
      MockitoAnnotations.initMocks(this);
      server = new MockWebServer();
      serviceHelper = new ServiceHelper();
  
  }
  @Test
  public void testEventBusIdPostedOnSuccessfulServiceCall() throws Exception {
      server.start();
      server.enqueue(new MockResponse().setResponseCode(200).setBody(getStringFromFile(RuntimeEnvironment.application, "list_success.json")));
      MainActivity.URL = server.url("/").toString();
  
      serviceHelper.getIndividualData("Store","7");
      verify(eventBus).post(any());
  }
  
  @After
  public void tearDown() throws Exception{
      server.shutdown();
  }
  </code></pre>
  
  <p>Everything works fine. The retrofit call which is async is triggered with the mock JSON. However, the callback never gets triggered. I tried putting in a countdown latch within the actual service implementation, but nothing happens either.</p>
  
  <pre><code>    public void getIndividualData(String item, String number) {
  
              Call&lt;DataList&gt; dataList = RestClient.get().getList(item, number);
              dataList.enqueue(new Callback&lt;DataList&gt;() {
                  @Override
                  public void onResponse(Response&lt;DataList&gt; response, Retrofit retrofit) {
                      /*data persistence should take place before sending out the eventbus message.
                       Passing the response object directly for sample purpose. */
                      EventBus.getDefault().post(new IndividualItemEvent(response.body().Values));
                  //    latch.countDown(); ----&gt;might be needed for retrofit async unit testing
                  }
  
                  @Override
                  public void onFailure(Throwable t) {
                      Log.d("%%%%%", "retrofit failure");
                     // latch.countDown(); ----&gt;might be needed for retrofit async unit testing
                  }
              });
  //        try {
  //            latch.await();
  //        } catch (InterruptedException e) { ----&gt;might be needed for retrofit async unit testing
  //            e.printStackTrace();
  //        }
  </code></pre>
  
  <p>Am i missing something here?
  Thanks.</p>