From 605eedff8e66d437bb8f7bb8a00d0a830ff7d2c2 Mon Sep 17 00:00:00 2001 From: Auto_EPMD-EDP AIAssistant Date: Fri, 25 Oct 2024 11:45:42 +0300 Subject: [PATCH] Add unit test for findOwner method in PetController to handle IllegalArgumentException scenario --- .../petclinic/owner/PetControllerTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTest.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTest.java index eef2adedb..cbcf8989c 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTest.java @@ -17,6 +17,8 @@ import java.util.Collection; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.doThrow; +import org.junit.jupiter.api.Assertions; @ExtendWith(MockitoExtension.class) @DisplayName("PetController Tests") @@ -52,4 +54,17 @@ class PetControllerTest { verify(ownerRepository).findPetTypes(); } + @Test + @DisplayName("Test findOwner throws IllegalArgumentException") + void testFindOwnerThrowsIllegalArgumentException() { + int ownerId = 999; + doThrow(new IllegalArgumentException()).when(ownerRepository).findById(ownerId); + + Assertions.assertThrows(IllegalArgumentException.class, () -> { + petController.findOwner(ownerId); + }); + + verify(ownerRepository).findById(ownerId); + } + }