PetFinder { public Pet findById(String petId) { return petRepository.findById(petId).orElseThrow(() -> new PetNotFoundException(petId)); } PetFinder(PetRepository petRepository); Pet findById(String petId);  }
@Test void shouldReturnAPet_whenFound() { var pet = new Pet(); pet.setPetId(PET_ID); BDDMockito.given(petRepository.findById(PET_ID)).willReturn(Optional.of(pet)); var petFound = petFinder.findById(PET_ID); Assertions.assertNotNull(petFound); Assertions.assertEquals(PET_ID, petFound.getPetId()); }
@Test void shouldThrowPetNotFoundException_whenNotFound() { BDDMockito.given(petRepository.findById(PET_ID)).willReturn(Optional.empty()); Assertions.assertThrows(PetNotFoundException.class, () -> petFinder.findById(PET_ID)); }