title: How to write JUnit test for a method that calls API?
body: <p>I've to write test for a class that calls an API and then processes the response. The class has two public functions and a private function. The first public method fetches a list of IDs. The second public method is called in a loop for every ID to get details associated with an ID. The private method is called inside the second public method, since the calls to fetch details based on id are made asynchronously. </p>
  
  <p>I'm new to JUnits and while I understand that I should not test the API calls, just my functions, I still don't understand what should the unit tests assert.</p>
  
  <p>Below are my functions:</p>
  
  <pre><code>public List&lt;Integer&gt; fetchVehicleIds(String datasetId) throws ApiException {
  
      VehiclesApi vehiclesApi = new VehiclesApi();
  
      List&lt;Integer&gt; vehicleIds;
      vehicleIds = vehiclesApi.vehiclesGetIds(datasetId).getVehicleIds();
  
      return vehicleIds;
  }
  
   public List&lt;VehicleResponse&gt; fetchVehicleDetails(String datasetId, List&lt;Integer&gt; vehicleIds) throws InterruptedException, ApiException {
  
      CountDownLatch latch = new CountDownLatch(vehicleIds.size());
      List&lt;VehicleResponse&gt; vehiclesList = new ArrayList&lt;&gt;();
  
      for (Integer vehicleId: vehicleIds) {
          populateEachVehicleDetail(datasetId, vehicleId, vehiclesList, latch);
      }
  
      latch.await();
  
      return vehiclesList;
  }
  
  private void populateEachVehicleDetail(String datasetId, Integer vehicleId, List&lt;VehicleResponse&gt; vehiclesList, CountDownLatch latch) throws ApiException {
  
      ApiCallback&lt;VehicleResponse&gt; vehicleResponseApiCallback = new ApiCallback&lt;VehicleResponse&gt;() {
          @Override
          synchronized public void onSuccess(VehicleResponse result, int statusCode, Map&lt;String, List&lt;String&gt;&gt; responseHeaders) {
              vehiclesList.add(result);
              latch.countDown();
          }
      };
  
      VehiclesApi vehiclesApi = new VehiclesApi();
      vehiclesApi.vehiclesGetVehicleAsync(datasetId,vehicleId,vehicleResponseApiCallback);
  
  }
  </code></pre>
  
  <p>Based on the research I've done so far, I think I have to mock the API calls using mockito? I'm still unclear on how the functionality can be unit tested.</p>