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);  }
@Test void remove_created_vehicle_by_id() { VehicleEntity vehicleEntity = vehicleEntity(testVehicle); final long id = testVehicle.id(); when(crudRepository.findById(id)).thenReturn(Optional.of(vehicleEntity)); Vehicle removed = repository.removeVehicle(id); assertThat(removed).isEqualTo(testVehicle); verify(crudRepository).deleteById(id); }
@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)); }