mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 06:45:50 +00:00
Add unit test for populatePetTypes method in PetController
This commit is contained in:
parent
d128ab9343
commit
69cdebe4e7
1 changed files with 55 additions and 0 deletions
|
@ -0,0 +1,55 @@
|
||||||
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.samples.petclinic.owner.PetController;
|
||||||
|
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
||||||
|
import org.springframework.samples.petclinic.owner.PetType;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@DisplayName("PetController Tests")
|
||||||
|
class PetControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private OwnerRepository ownerRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private PetController petController;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test populatePetTypes")
|
||||||
|
void testPopulatePetTypes() {
|
||||||
|
PetType type1 = new PetType();
|
||||||
|
type1.setId(1);
|
||||||
|
type1.setName("Dog");
|
||||||
|
|
||||||
|
PetType type2 = new PetType();
|
||||||
|
type2.setId(2);
|
||||||
|
type2.setName("Cat");
|
||||||
|
|
||||||
|
doReturn(Arrays.asList(type1, type2)).when(ownerRepository).findPetTypes();
|
||||||
|
|
||||||
|
Collection<PetType> petTypes = petController.populatePetTypes();
|
||||||
|
|
||||||
|
assertThat(petTypes).hasSize(2);
|
||||||
|
assertThat(petTypes).contains(type1, type2);
|
||||||
|
verify(ownerRepository).findPetTypes();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue