@Test void removing_nonexistent_vehicle_should_fail() { when(crudRepository.findById(anyLong())).thenReturn(Optional.empty()); int uniqueNonexistentId = 7173; assertThatIllegalArgumentException() .isThrownBy(() -> repository.removeVehicle(uniqueNonexistentId)) .withMessageContaining(String.valueOf(uniqueNonexistentId)); }
@Override public Vehicle removeVehicle(long id) { Optional<VehicleEntity> optionalVehicleEntity = repository.findById(id); VehicleEntity vehicleEntity = optionalVehicleEntity.orElseThrow( () -> new IllegalArgumentException("Vehicle{id=" + id + "} doesn't exist")); repository.deleteById(id); Vehicle vehicle = toDomain(vehicleEntity); logger.info("Deleted vehicle {}.", vehicle); return vehicle; }
VehicleRepositoryImpl implements VehicleRepository { @Override public Vehicle removeVehicle(long id) { Optional<VehicleEntity> optionalVehicleEntity = repository.findById(id); VehicleEntity vehicleEntity = optionalVehicleEntity.orElseThrow( () -> new IllegalArgumentException("Vehicle{id=" + id + "} doesn't exist")); repository.deleteById(id); Vehicle vehicle = toDomain(vehicleEntity); logger.info("Deleted vehicle {}.", vehicle); return vehicle; } }
VehicleRepositoryImpl implements VehicleRepository { @Override public Vehicle removeVehicle(long id) { Optional<VehicleEntity> optionalVehicleEntity = repository.findById(id); VehicleEntity vehicleEntity = optionalVehicleEntity.orElseThrow( () -> new IllegalArgumentException("Vehicle{id=" + id + "} doesn't exist")); repository.deleteById(id); Vehicle vehicle = toDomain(vehicleEntity); logger.info("Deleted vehicle {}.", vehicle); return vehicle; } VehicleRepositoryImpl(VehicleCrudRepository repository); }
VehicleRepositoryImpl implements VehicleRepository { @Override public Vehicle removeVehicle(long id) { Optional<VehicleEntity> optionalVehicleEntity = repository.findById(id); VehicleEntity vehicleEntity = optionalVehicleEntity.orElseThrow( () -> new IllegalArgumentException("Vehicle{id=" + id + "} doesn't exist")); repository.deleteById(id); Vehicle vehicle = toDomain(vehicleEntity); logger.info("Deleted vehicle {}.", vehicle); return vehicle; } VehicleRepositoryImpl(VehicleCrudRepository repository); @Override Vehicle createVehicle(int capacity); @Override Vehicle createVehicle(VehicleData vehicleData); @Override List<Vehicle> vehicles(); @Override Vehicle removeVehicle(long id); @Override void removeAll(); @Override Optional<Vehicle> find(long vehicleId); @Override void update(Vehicle vehicle); }
VehicleRepositoryImpl implements VehicleRepository { @Override public Vehicle removeVehicle(long id) { Optional<VehicleEntity> optionalVehicleEntity = repository.findById(id); VehicleEntity vehicleEntity = optionalVehicleEntity.orElseThrow( () -> new IllegalArgumentException("Vehicle{id=" + id + "} doesn't exist")); repository.deleteById(id); Vehicle vehicle = toDomain(vehicleEntity); logger.info("Deleted vehicle {}.", vehicle); return vehicle; } VehicleRepositoryImpl(VehicleCrudRepository repository); @Override Vehicle createVehicle(int capacity); @Override Vehicle createVehicle(VehicleData vehicleData); @Override List<Vehicle> vehicles(); @Override Vehicle removeVehicle(long id); @Override void removeAll(); @Override Optional<Vehicle> find(long vehicleId); @Override void update(Vehicle vehicle);  }