CheckDataService { City findClosestCity(Location location, List<City> cities) { City closestCity = null; float closestDistance = Float.MAX_VALUE; if (location != null && cities != null) { for (City city : cities) { float[] results = new float[1]; Location.distanceBetween(city.lat, city.lng, location.getLatitude(), location.getLongitude(), results); if (results[0] < closestDistance) { closestCity = city; closestDistance = results[0]; } } } return closestCity; } @Inject  CheckDataService(Preferences prefs, CitiesDao citiesDao, BaggersDao baggersDao, LocationsDao locationsDao, LocationProvider locationProvider); Observable<Pair<Boolean, City>> checkData(Context context);  }
@Test public void should_return_closest_city() { when(location.getLatitude()).thenReturn(24.4); when(location.getLongitude()).thenReturn(12.0); City city1 = new City(); city1.lat = 42.0; city1.lng = 2.1; City city2 = new City(); city2.lat = 24.4; city2.lng = 12.2; City city3 = new City(); city3.lat = 7.2; city3.lng = 14.3; City closestCity = service.findClosestCity(location, Arrays.asList(city1, city2, city3)); assertThat(closestCity).isEqualTo(city2); }
@Test public void should_return_null_if_location_or_cities_are_null() { List<City> cities = new ArrayList<>(); City closestCity1 = service.findClosestCity(null, cities); City closestCity2 = service.findClosestCity(location, null); assertThat(closestCity1).isEqualTo(closestCity2); assertThat(closestCity1).isNull(); }