### Question:
DomainService { public Optional<Domain> create(Domain domain) throws ValidationException { if (read(domain.getKey()).isPresent()) { throw new ValidationException("Can't create because it already exists"); } domainValidator.validateForCreate(domain); domain.setSpecification(handlerService.handleInsert(domain)); 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 create() { final Domain entity = mock(Domain.class); final DomainKey key = mock(DomainKey.class); final Specification specification = mock(Specification.class); Mockito.when(domainRepository.findById(key)).thenReturn(Optional.empty()); Mockito.doNothing().when(domainValidator).validateForCreate(entity); Mockito.when(handlerService.handleInsert(entity)).thenReturn(specification); Mockito.when(domainRepository.save(any())).thenReturn(entity); when(entity.getKey()).thenReturn(key); domainService.create(entity); verify(entity).getKey(); verify(domainRepository).findById(key); verify(domainValidator).validateForCreate(entity); verify(handlerService).handleInsert(entity); verify(domainRepository).save(entity); }