mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 06:45:50 +00:00
Add unit tests for PetTypeFormatter parse method
This commit is contained in:
parent
bed602903c
commit
9359be811a
1 changed files with 59 additions and 0 deletions
|
@ -0,0 +1,59 @@
|
||||||
|
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.PetType;
|
||||||
|
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
|
||||||
|
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.BDDMockito.then;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class PetTypeFormatterTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private OwnerRepository ownerRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private PetTypeFormatter petTypeFormatter;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test parse method with valid pet type")
|
||||||
|
void testParseWithValidPetType() throws Exception {
|
||||||
|
List<PetType> petTypes = new ArrayList<>();
|
||||||
|
PetType dog = new PetType();
|
||||||
|
dog.setName("Dog");
|
||||||
|
petTypes.add(dog);
|
||||||
|
given(ownerRepository.findPetTypes()).willReturn(petTypes);
|
||||||
|
|
||||||
|
PetType result = petTypeFormatter.parse("Dog", Locale.ENGLISH);
|
||||||
|
|
||||||
|
assertThat(result.getName()).isEqualTo("Dog");
|
||||||
|
then(ownerRepository).should().findPetTypes();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test parse method with invalid pet type")
|
||||||
|
void testParseWithInvalidPetType() throws Exception {
|
||||||
|
List<PetType> petTypes = new ArrayList<>();
|
||||||
|
PetType dog = new PetType();
|
||||||
|
dog.setName("Dog");
|
||||||
|
petTypes.add(dog);
|
||||||
|
given(ownerRepository.findPetTypes()).willReturn(petTypes);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> petTypeFormatter.parse("Dragon", Locale.ENGLISH)).isInstanceOf(ParseException.class)
|
||||||
|
.hasMessage("type not found: Dragon");
|
||||||
|
then(ownerRepository).should().findPetTypes();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue