mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 06:45:50 +00:00
Add unit test for findOwner method in PetController
This commit is contained in:
parent
f8d1c6e23d
commit
36398d71db
1 changed files with 43 additions and 0 deletions
|
@ -49,7 +49,10 @@ import static org.mockito.Mockito.verify;
|
|||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@DisplayName("OwnerController Tests")
|
||||
class OwnerControllerTest {
|
||||
|
||||
@Mock
|
||||
|
@ -208,6 +211,46 @@ class OwnerControllerTest {
|
|||
|
||||
}
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PetControllerTest {
|
||||
|
||||
@Mock
|
||||
private OwnerRepository ownerRepository;
|
||||
|
||||
@InjectMocks
|
||||
private PetController petController;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(petController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test findOwner with valid ownerId")
|
||||
void testFindOwnerWithValidOwnerId() {
|
||||
int ownerId = 1;
|
||||
Owner owner = new Owner();
|
||||
doReturn(owner).when(ownerRepository).findById(ownerId);
|
||||
|
||||
Owner result = petController.findOwner(ownerId);
|
||||
|
||||
assertThat(result).isEqualTo(owner);
|
||||
verify(ownerRepository).findById(ownerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test findOwner with invalid ownerId")
|
||||
void testFindOwnerWithInvalidOwnerId() {
|
||||
int ownerId = 999;
|
||||
doReturn(null).when(ownerRepository).findById(ownerId);
|
||||
|
||||
assertThatThrownBy(() -> petController.findOwner(ownerId)).isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Owner ID not found: " + ownerId);
|
||||
verify(ownerRepository).findById(ownerId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class VisitControllerTest {
|
||||
|
||||
|
|
Loading…
Reference in a new issue