VehicleRepositoryImpl implements VehicleRepository { @Override public Vehicle createVehicle(int capacity) { long id = repository.save(new VehicleEntity(0, null, capacity)).getId(); VehicleEntity vehicleEntity = repository.save(new VehicleEntity(id, "Vehicle " + id, capacity)); Vehicle vehicle = toDomain(vehicleEntity); logger.info("Created 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 should_create_vehicle_and_generate_id_and_name() { VehicleEntity newEntity = vehicleEntity(testVehicle); when(crudRepository.save(vehicleEntityCaptor.capture())).thenReturn(newEntity); int savedCapacity = 1; Vehicle newVehicle = repository.createVehicle(savedCapacity); List<VehicleEntity> savedVehicles = vehicleEntityCaptor.getAllValues(); assertThat(savedVehicles).hasSize(2); assertThat(savedVehicles.get(0).getName()).isNull(); assertThat(savedVehicles.get(0).getCapacity()).isEqualTo(savedCapacity); assertThat(savedVehicles.get(1).getName()).isEqualTo("Vehicle " + newEntity.getId()); assertThat(savedVehicles.get(1).getCapacity()).isEqualTo(savedCapacity); assertThat(newVehicle.id()).isEqualTo(newEntity.getId()); assertThat(newVehicle.name()).isEqualTo(newEntity.getName()); assertThat(newVehicle.capacity()).isEqualTo(newEntity.getCapacity()); }
@Test void create_vehicle_from_given_data() { VehicleEntity newEntity = vehicleEntity(testVehicle); when(crudRepository.save(vehicleEntityCaptor.capture())).thenReturn(newEntity); VehicleData vehicleData = VehicleFactory.vehicleData("x", 1); Vehicle newVehicle = repository.createVehicle(vehicleData); VehicleEntity savedVehicle = vehicleEntityCaptor.getValue(); assertThat(savedVehicle.getName()).isEqualTo(vehicleData.name()); assertThat(savedVehicle.getCapacity()).isEqualTo(vehicleData.capacity()); assertThat(newVehicle.id()).isEqualTo(newEntity.getId()); assertThat(newVehicle.name()).isEqualTo(newEntity.getName()); assertThat(newVehicle.capacity()).isEqualTo(newEntity.getCapacity()); }