Added TestPetTypeFormatter test class

tests the PetTypeFormatter by breaking teh dependencies with PetTypeRepository and PetType using mocks
Also renames the folder to teamkoganM2additions
This commit is contained in:
Zackkogan 2018-02-25 15:29:53 -05:00
parent 9657254791
commit a839859a3f
2 changed files with 79 additions and 6 deletions

View file

@ -0,0 +1,73 @@
package org.springframework.samples.petclinic.teamkoganM2additions;
import org.junit.Test;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
import static org.mockito.Mockito.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
public class TestPetTypeFormatter {
@Test
public void testPetTypeFormatter() {
//create the mock PetRepository
PetRepository mockPetRepository = mock(PetRepository.class);
//create the mock PetType
PetType mockPetType = mock(PetType.class);
//call the class under test
PetTypeFormatter PTF = new PetTypeFormatter(mockPetRepository);
//stub the getName() method
when(mockPetType.getName()).thenReturn("Testy");
//test the print method
String name = PTF.print(mockPetType, Locale.ENGLISH);
//check the result is what we expected
assertEquals("Testy", name);
//verify the getName method was called
verify(mockPetType).getName();
//stub the findPetTypes method to return the PetTypes defined below
when(mockPetRepository.findPetTypes()).thenReturn(PetTypes());
//try catch block needed for ParseException that can be thrown when testing the parse method
try {
//test the parse method
PetType testPetType = PTF.parse("Pupper", Locale.ENGLISH);
//verify the getName method was called
verify(mockPetType).getName();
//check that the result is what is expected
assertEquals("Pupper", testPetType.getName());
} catch (ParseException e) {
e.printStackTrace();
}
}
//define some pet types
private List<PetType> PetTypes() {
List<PetType> petTypes = new ArrayList<>();
petTypes.add(new PetType() {
{ setName("Doggo"); }
});
petTypes.add(new PetType() {
{ setName("Pupper"); }
});
return petTypes;
}
}

View file

@ -1,4 +1,4 @@
package org.springframework.samples.petclinic.teamkogan; package org.springframework.samples.petclinic.teamkoganM2additions;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -20,18 +20,18 @@ public class VisitControllerTest
// Define random pet ID // Define random pet ID
final Integer PET_ID = 7; final Integer PET_ID = 7;
Pet pet7 = new Pet(); Pet pet7 = new Pet();
// Mock dependencies // Mock dependencies
VisitRepository mockVisitRepo = mock(VisitRepository.class); VisitRepository mockVisitRepo = mock(VisitRepository.class);
PetRepository mockPetRepo = mock(PetRepository.class); PetRepository mockPetRepo = mock(PetRepository.class);
VisitController visitController = new VisitController(mockVisitRepo, mockPetRepo); VisitController visitController = new VisitController(mockVisitRepo, mockPetRepo);
// Define mock behaviour // Define mock behaviour
when(mockPetRepo.findById(PET_ID)).thenReturn(pet7); when(mockPetRepo.findById(PET_ID)).thenReturn(pet7);
// Call method under inspection // Call method under inspection
Visit visit = visitController.loadPetWithVisit(PET_ID, new HashMap<>()); Visit visit = visitController.loadPetWithVisit(PET_ID, new HashMap<>());
// Confirm that the same visit date was assigned to the pet // Confirm that the same visit date was assigned to the pet
assertEquals(visit.getDate(), pet7.getVisits().get(0).getDate()); assertEquals(visit.getDate(), pet7.getVisits().get(0).getDate());
} }