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