DistanceRepositoryImpl implements DistanceRepository { @Override public long getDistance(Location from, Location to) { Optional<DistanceEntity> optional = distanceRepository.findById(new DistanceKey(from.id(), to.id())); if (optional.isPresent()) { return optional.get().getDistance(); } return -1; } @Autowired  DistanceRepositoryImpl(DistanceCrudRepository distanceRepository); @Override void saveDistance(Location from, Location to, long distance); @Override long getDistance(Location from, Location to); @Override void deleteDistances(Location location); @Override void deleteAll();  }
@Test void should_return_distance_when_entity_is_found() { DistanceKey distanceKey = new DistanceKey(from.id(), to.id()); long distance = 10305; DistanceEntity distanceEntity = new DistanceEntity(distanceKey, distance); when(crudRepository.findById(distanceKey)).thenReturn(Optional.of(distanceEntity)); assertThat(repository.getDistance(from, to)).isEqualTo(distance); }
@Test void should_return_negative_number_when_distance_not_found() { when(crudRepository.findById(any(DistanceKey.class))).thenReturn(Optional.empty()); assertThat(repository.getDistance(from, to)) .isNegative() .isNotZero(); }