### Question:
InfrastructureService { public Optional<Infrastructure> update(Infrastructure infrastructure) throws ValidationException { var existing = read(infrastructure.getKey()); if (!existing.isPresent()) { throw new ValidationException("Can't update " + infrastructure.getKey().getName() + " because it doesn't exist"); } infrastructureValidator.validateForUpdate(infrastructure, existing.get()); infrastructure.setSpecification(handlerService.handleUpdate(infrastructure, existing.get())); return save(infrastructure); }  Optional<Infrastructure> create(Infrastructure infrastructure); Optional<Infrastructure> update(Infrastructure infrastructure); Optional<Infrastructure> upsert(Infrastructure infrastructure); Optional<Infrastructure> read(InfrastructureKey key); List<Infrastructure> findAll(Predicate<Infrastructure> filter); void delete(Infrastructure infrastructure); boolean exists(InfrastructureKey key);  }### Answer:
@Test public void update() { final Infrastructure entity = mock(Infrastructure.class); final InfrastructureKey key = mock(InfrastructureKey.class); final Infrastructure existingEntity = mock(Infrastructure.class); final Specification specification = mock(Specification.class); when(entity.getKey()).thenReturn(key); when(infrastructureRepository.findById(key)).thenReturn(Optional.of(existingEntity)); doNothing().when(infrastructureValidator).validateForUpdate(entity, existingEntity); when(handlerService.handleUpdate(entity, existingEntity)).thenReturn(specification); when(infrastructureRepository.save(entity)).thenReturn(entity); infrastructureService.update(entity); verify(entity).getKey(); verify(infrastructureRepository).findById(key); verify(infrastructureValidator).validateForUpdate(entity, existingEntity); verify(handlerService).handleUpdate(entity, existingEntity); verify(infrastructureRepository).save(entity); }