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