GoogleLocationAdapter implements CTLocationAdapter { @WorkerThread @Override public void getLastLocation(@NonNull final CTLocationCallback callback) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Requesting Last Location.."); Location location = null; try { @SuppressLint("MissingPermission") Task<Location> lastLocation = fusedProviderClient.getLastLocation(); location = Tasks.await(lastLocation); if (location != null) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "New Location = " + location.getLatitude() + "," + location.getLongitude()); } CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Last location request completed"); } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Failed to request last location"); e.printStackTrace(); } finally { callback.onLocationComplete(location); } } GoogleLocationAdapter(@NonNull Context context); @WorkerThread @Override void getLastLocation(@NonNull final CTLocationCallback callback); @WorkerThread @Override void removeLocationUpdates(@Nullable PendingIntent pendingIntent); @WorkerThread @Override void requestLocationUpdates();  }
@Test public void testGetLastLocation() { final Location expectedLocation = GeofenceEventFake.getTriggeredLocation(); when(LocationServices.getFusedLocationProviderClient(application)) .thenReturn(providerClient); Task<Location> locationTask = mock(Task.class); try { PowerMockito.when(Tasks.await(locationTask)).thenReturn(expectedLocation); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } when(providerClient.getLastLocation()).thenReturn(locationTask); final GoogleLocationAdapter locationAdapter = new GoogleLocationAdapter(application); locationAdapter.getLastLocation(new CTLocationCallback() { @Override public void onLocationComplete(Location location) { Assert.assertSame(expectedLocation, location); } }); }