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