PetOrderer { public Order order(String petId) { var pet = petRepository.findById(petId).orElseThrow(() -> new PetNotFoundException(petId)); if (PetStatus.AVAILABLE != pet.getStatus()) { throw new PetNotAvailableException(petId); } var order = new Order(); order.setOrderId(idGenerator.generate()); order.setPet(pet); order.setStatus(OrderStatus.PLACED); return orderRepository.save(order); } PetOrderer(
			IdGenerator idGenerator,
			PetRepository petRepository,
			OrderRepository orderRepository
	); Order order(String petId);  }
@Test void shouldThrowPetNotFoundException_whenPetIsNotFound() { BDDMockito.given(petRepository.findById(PET_ID)).willReturn(Optional.empty()); Assertions.assertThrows(PetNotFoundException.class, () -> petOrderer.order(PET_ID)); }
@Test void shouldThrowPetNotAvailable_whenPetIsNotAvailable() { var pet = new Pet(); pet.setStatus(PetStatus.SOLD); BDDMockito.given(petRepository.findById(PET_ID)).willReturn(Optional.of(pet)); Assertions.assertThrows(PetNotAvailableException.class, () -> petOrderer.order(PET_ID)); }
@Test void shouldReturnAndOrder_whenPetIsFound_andPetStatusIsAdequate() { BDDMockito.given(idGenerator.generate()).willReturn(ID); var pet = new Pet(); pet.setStatus(PetStatus.AVAILABLE); BDDMockito.given(petRepository.findById(PET_ID)).willReturn(Optional.of(pet)); BDDMockito.given(orderRepository.save(Mockito.any(Order.class))) .willAnswer(invocation -> invocation.getArgument(0)); var order = petOrderer.order(PET_ID); Assertions.assertNotNull(order); Assertions.assertEquals(ID, order.getOrderId()); Assertions.assertEquals(OrderStatus.PLACED, order.getStatus()); }