This commit is contained in:
mnhn329 2018-02-25 20:58:40 +00:00 committed by GitHub
commit b5f412d569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 634 additions and 2 deletions

View file

@ -23,3 +23,6 @@ logging.level.org.springframework=INFO
# Active Spring profiles
spring.profiles.active=production
# Adjusting local host to 8090 for application launch
server.port = 8090

View file

@ -0,0 +1,30 @@
package org.springframework.samples.petclinic.model;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class BaseEntityTest {
private BaseEntity baseEntityInstance;
@Before
public void setUp(){
baseEntityInstance = new BaseEntity();
}
// Expects the baseEntity to have Integer of 100
@Test
public void testGetAndSetId() {
baseEntityInstance.setId(100);
assertEquals((Integer)100, baseEntityInstance.getId());
}
// Expects the baseEntity ID to be null
@Test
public void TestIsNew() {
baseEntityInstance.isNew();
assertNull(baseEntityInstance.getId());
}
}

View file

@ -0,0 +1,26 @@
package org.springframework.samples.petclinic.model;
import org.junit.Before;
import org.junit.Test;
import javax.naming.Name;
import static org.junit.Assert.*;
public class NamedEntityTest {
private NamedEntity namedObj;
@Before
public void setUp(){
namedObj = new NamedEntity();
}
// Testing the DAO of NamedEntity class by verifying the setter, getter and toString of Name
@Test
public void testSetAndGetName() {
namedObj.setName("Junior The Senior");
assertEquals("Junior The Senior", namedObj.getName());
assertEquals("Junior The Senior", namedObj.toString());
}
}

View file

@ -0,0 +1,30 @@
package org.springframework.samples.petclinic.model;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class PersonTest {
private Person personObj;
@Before
public void setUp(){
personObj = new Person();
}
// Testing the DAO of Person class by verifying the setter and getter of FirstName
@Test
public void testSetAndGetFirstName() {
personObj.setFirstName("Johnny");
assertEquals("Johnny", personObj.getFirstName());
}
// Testing the DAO of Person class by verifying the setter and getter of FirstName
@Test
public void testSetAndGetLastName() {
personObj.setLastName("Oliver");
assertEquals("Oliver", personObj.getLastName());
}
}

View file

@ -0,0 +1,113 @@
package org.springframework.samples.petclinic.owner;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.style.ToStringCreator;
public class OwnerTest {
private Owner ownerInstance;
@Before
public void setUp() {
this.ownerInstance = new Owner();
}
@Test
public void getSetTelephoneTest() {
// Owner instance = new Owner();
ownerInstance.setTelephone("514 371 9999");
String result = ownerInstance.getTelephone();
assertEquals("514 371 9999", result);
}
@Test
public void setGetCityTest() {
// Owner instance = new Owner();
ownerInstance.setCity("Montreal");
String result = ownerInstance.getCity();
assertEquals("Montreal", result);
}
@Test
public void toStringTest() {
ToStringCreator creator = new ToStringCreator(ownerInstance);
String expected =
creator
.append("id", ownerInstance.getId())
.append("new", ownerInstance.isNew())
.append("lastName", ownerInstance.getLastName())
.append("firstName", ownerInstance.getFirstName())
.append("address", ownerInstance.getAddress())
.append("city", ownerInstance.getCity())
.append("telephone", ownerInstance.getTelephone())
.toString();
String result = ownerInstance.toString();
assertEquals(expected, result);
}
@Test
public void setPetgetPetsTest() {
Pet pet = new Pet();
pet.setName("Pogo");
ownerInstance.addPet(pet);
List<Pet> result = ownerInstance.getPets();
Pet onlyPet = result.iterator().next();
assertEquals(1, result.size()); // Make sure there's only one element in the Collection returned
assertEquals(pet, onlyPet);
assertEquals(pet.getName(), onlyPet.getName());
}
@Test
public void getPetExistsTest() {
Pet pet = new Pet();
pet.setName("Pochi");
ownerInstance.addPet(pet);
//tests pet object exists
assertEquals(pet, ownerInstance.getPet("Pochi"));
assertEquals(pet, ownerInstance.getPet("Pochi", false));
}
@Test
public void getPetDoesntExistsTest() {
Pet pet = new Pet();
pet.setName("Pochi");
ownerInstance.addPet(pet);
//tests pet object doesn't exist
assertEquals(null, ownerInstance.getPet("Pochi", true));
}
@Test
public void getPetsTest() {
Pet pet = new Pet();
List<Pet> list = new ArrayList<>();
list.add(pet);
ownerInstance.addPet(pet);
assertEquals(list, ownerInstance.getPets());
assertEquals(1, list.size());
Pet pet2 = new Pet();
list.add(pet2);
ownerInstance.addPet(pet2);
assertEquals(list, ownerInstance.getPets());
assertEquals(2, list.size());
}
@Test
public void setGetAddress() {
ownerInstance.setAddress("123 FakeStreet");
assertEquals("123 FakeStreet", ownerInstance.getAddress());
}
}

View file

@ -0,0 +1,98 @@
package org.springframework.samples.petclinic.owner;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
public class PetControllerMockito {
private PetRepository pets;
private OwnerRepository owners;
private PetController controller;
private BindingResult result;
private ModelMap model;
private Owner owner;
private Pet pet;
private final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
@Before
public void setup() {
pets = mock(PetRepository.class);
owners = mock(OwnerRepository.class);
result = mock(BindingResult.class);
model = mock(ModelMap.class);
owner = mock(Owner.class);
pet = mock(Pet.class);
controller = new PetController(pets, owners);
}
//Verifies method calls when a pet already exists
@Test
public void testCreationFormError() {
when(pet.getName()).thenReturn("FIDO");
when(pet.isNew()).thenReturn(true);
when(owner.getPet("FIDO", true)).thenReturn(pet);
when(result.hasErrors()).thenReturn(true);
String redirect = controller.processCreationForm(owner, pet, result, model);
//Pet should be added to owner
verify(owner).addPet(pet);
//Should have an error of a pet with duplicate name
verify(result).rejectValue("name", "duplicate", "already exists");
//Should set the "pet" of the redirect
verify(model).put("pet", pet);
//Should not save the pet
verify(pets, times(0)).save(anyObject());
//should redirect to create/update form
assertEquals(redirect, VIEWS_PETS_CREATE_OR_UPDATE_FORM);
}
@Test
public void testCreationFormSuccess() {
when(pet.getName()).thenReturn("FIDO");
when(pet.isNew()).thenReturn(false);
when(result.hasErrors()).thenReturn(false);
String redirect = controller.processCreationForm(owner, pet, result, model);
//Should not make an error
verify(result, times(0)).rejectValue(anyString(), anyString(), anyString());
//Should save the pet to repository
verify(pets).save(pet);
//Should redirect to a pet page
assertEquals(redirect, "redirect:/owners/{ownerId}");
}
@Test
public void testUpdateFormError() {
when(result.hasErrors()).thenReturn(true);
String redirect = controller.processUpdateForm(pet, result, owner, model);
//Pet's owner should be set
verify(pet).setOwner(owner);
//Should set the "pet" of the redirect
verify(model).put("pet", pet);
assertEquals(redirect, VIEWS_PETS_CREATE_OR_UPDATE_FORM);
}
@Test
public void testUpdateFormSuccess() {
when(result.hasErrors()).thenReturn(false);
String redirect = controller.processUpdateForm(pet, result, owner, model);
//Pet should be added to owner
verify(owner).addPet(pet);
//Should save the pet to repository
verify(pets).save(pet);
//Should redirect to an owner page
assertEquals(redirect, "redirect:/owners/{ownerId}");
}
}

View file

@ -0,0 +1,86 @@
package org.springframework.samples.petclinic.owner;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.samples.petclinic.visit.Visit;
import java.util.List;
import java.util.Set;
import java.util.LinkedHashSet;
import java.time.*;
import java.util.Date;
public class PetTests {
private Pet pet;
private Date birthDate;
@Before
public void testSetUp() {
//Initialization of pet
this.pet = new Pet();
PetType dog = new PetType();
dog.setId(9);
dog.setName("Duncan Jones");
//Initialization of birthDate
//Converting the current time to a local date and ultimately a date to be input into setBirthDate;
LocalDateTime timePoint = LocalDateTime.now();
LocalDate localDate = timePoint.toLocalDate();
birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
@Test
public void testSetAndGetBirthDate() {
pet.setBirthDate(this.birthDate);
Date resultOfGetDate = pet.getBirthDate();
assertEquals(this.birthDate, resultOfGetDate);
}
@Test
public void testSetAndGetType() {
//Creating a new pet type to test the setters and getters for pet's type
PetType walrus = new PetType();
walrus.setId(36);
walrus.setName("Alex Garland");
pet.setType(walrus);
PetType resultOfGetType = pet.getType();
assertEquals(walrus.getName(), resultOfGetType.getName());
}
@Test
public void testSetAndGetOwner() {
//Creating a new owner type to test the setters and getters for the pet's owner
Owner amandeepBhandal = new Owner();
amandeepBhandal.setAddress("Off-world Colony");
amandeepBhandal.setCity("Beirut");
amandeepBhandal.setTelephone("514-333-3333");
//Attach the newly created owner to the pet
pet.setOwner(amandeepBhandal);
Owner resultOfGetOwner = pet.getOwner();
assertEquals(resultOfGetOwner.getAddress(), "Off-world Colony");
assertEquals(resultOfGetOwner.getCity(), "Beirut");
assertEquals(resultOfGetOwner.getTelephone(), "514-333-3333");
assertEquals(resultOfGetOwner.getPetsInternal().size(), 0);
}
@Test
public void testSetAndGetVisitsInternal() {
//Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits
Set<Visit> visitsForTesting = new LinkedHashSet<>();
pet.setVisitsInternal(visitsForTesting);
Set<Visit> resultOfGetVisitsInternal = pet.getVisitsInternal();
assertEquals(visitsForTesting.size(), resultOfGetVisitsInternal.size());
}
@Test
public void testAddVisitAndGetVisits() {
//Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits
Visit visitForTesting = new Visit();
pet.addVisit(visitForTesting);
List<Visit> resultOfGetVisits = pet.getVisits();
Visit onlyVisitInCollection = resultOfGetVisits.iterator().next();
assertEquals(1, resultOfGetVisits.size());
assertEquals(visitForTesting.getId(), onlyVisitInCollection.getId());
}
}

View file

@ -0,0 +1,112 @@
package org.springframework.samples.petclinic.owner;
import static org.junit.Assert.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import org.junit.Test;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
public class PetValidatorTests {
@Test
public void testValidationWithValidName() {
//given
PetValidator petValidator = new PetValidator();
Pet validPet = new Pet();
validPet.setName("Peter");
Errors errors = new BeanPropertyBindingResult(validPet, "");
//when
petValidator.validate(validPet, errors);
//then
assertNull(errors.getFieldError("name"));
}
@Test
public void testValidationWithInvalidName() {
//given
PetValidator petValidator = new PetValidator();
Pet invalidPet = new Pet();
invalidPet.setName("");
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
//when
petValidator.validate(invalidPet, errors);
//then
assertTrue(errors.hasErrors());
}
@Test
public void testValidationWithValidType() {
//given
PetValidator petValidator = new PetValidator();
Pet validPet = new Pet();
PetType tiger = new PetType();
tiger.setId(24);
validPet.setType(tiger);
Errors errors = new BeanPropertyBindingResult(validPet, "");
//when
petValidator.validate(validPet, errors);
//then
assertNull(errors.getFieldError("type"));
}
@Test
public void testValidationWithInvalidType() {
//given
PetValidator petValidator = new PetValidator();
Pet invalidPet = new Pet();
PetType emptyType = null;
invalidPet.setType(emptyType);
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
//when
petValidator.validate(invalidPet, errors);
//then
assertTrue(errors.hasErrors());
}
@Test
public void testValidationWithValidBirthDate() {
//given
PetValidator petValidator = new PetValidator();
Pet validPet = new Pet();
LocalDateTime timePoint = LocalDateTime.now();
LocalDate localDate = timePoint.toLocalDate();
Date birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
validPet.setBirthDate(birthDate);
Errors errors = new BeanPropertyBindingResult(validPet, "");
//when
petValidator.validate(validPet, errors);
//then
assertNull(errors.getFieldError("birthDate"));
}
@Test
public void testValidationWithInvalidBirthDate() {
//given
PetValidator petValidator = new PetValidator();
Pet invalidPet = new Pet();
Date birthDate = null;
invalidPet.setBirthDate(birthDate);
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
//when
petValidator.validate(invalidPet, errors);
//then
assertTrue(errors.hasErrors());
}
}

View file

@ -16,10 +16,15 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.VisitController;
import org.springframework.samples.petclinic.visit.Visit;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.verify;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
/**
* Test class for {@link VisitController}
*
@ -50,6 +55,7 @@ public class VisitControllerTests {
mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdateVisitForm"));
verify(pets).findById(anyInt()); //Ensure that a pet gets loaded per visit
}
@Test
@ -60,6 +66,8 @@ public class VisitControllerTests {
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}"));
verify(pets).findById(anyInt()); //Ensure that a pet gets loaded per visit
verify(visits).save(any(Visit.class)); //Ensure that upon success, the visit gets saved
}
@Test
@ -70,6 +78,6 @@ public class VisitControllerTests {
.andExpect(model().attributeHasErrors("visit"))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdateVisitForm"));
verify(pets).findById(anyInt()); //Ensure that a pet gets loaded per visit
}
}

View file

@ -14,7 +14,7 @@ public class ProductionConfigurationTests {
@Autowired
private VetRepository vets;
//Test error introduced by addition of 10 commits since forking of our repo from original repo
@Test
public void testFindAll() throws Exception {
vets.findAll();

View file

@ -17,9 +17,14 @@ package org.springframework.samples.petclinic.vet;
import org.junit.Test;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.util.SerializationUtils;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
@ -40,4 +45,37 @@ public class VetTests {
assertThat(other.getId()).isEqualTo(vet.getId());
}
@Test
public void testGetSpecialties(){
Vet vet = new Vet();
vet.setFirstName("Zaphod");
vet.setLastName("Beeblebrox");
vet.setId(123);
Specialty radiology = new Specialty();
Specialty urinalysis = new Specialty();
Specialty kinesiology = new Specialty();
vet.addSpecialty(radiology);
vet.addSpecialty(urinalysis);
//vet.addSpecialty(kinesiology);
List<Specialty> testList = vet.getSpecialties();
assertThat(testList).isEqualTo(vet.getSpecialties());
testList.getClass().getSimpleName().equals("UnmodifiableCollection");
}
@Test
public void testAddGetNrSpecialty(){
Vet vet = new Vet();
vet.setFirstName("Zaphod");
vet.setLastName("Beeblebrox");
vet.setId(123);
int preAddedNumber = vet.getNrOfSpecialties();
Specialty radiology = new Specialty();
//radiology.setName("radiology");
vet.addSpecialty(radiology);
assertThat(vet.getNrOfSpecialties()).isEqualTo(++preAddedNumber);
assertThat((vet.getSpecialtiesInternal()).contains(radiology));
}
}

View file

@ -0,0 +1,50 @@
package org.springframework.samples.petclinic.visit;
import java.util.Date;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
public class VisitRepositoryTest {
private static final int VISIT_ID = 1;
private static final String DESCRIPTION = "Visiting a test case";
private static final Date TODAY = new Date();
private Visit visit;
private List<Visit> visitList;
@Autowired
private VisitRepository visitRepository;
@Before
public void setUp(){
visit = new Visit();
visit.setDescription(DESCRIPTION);
visit.setPetId(VISIT_ID);
visit.setDate(TODAY);
}
@Test
public void shouldFindSavedVisitInVisitRepository() {
visitRepository.save(visit);
//Get the list of visits associated to VISIT_ID
visitList = visitRepository.findByPetId(VISIT_ID);
assertThat(visitList.size()).isEqualTo(1);
Visit savedVisit = visitList.get(0);
assertThat((int) savedVisit.getPetId()).isEqualTo(VISIT_ID);
assertThat(savedVisit.getDescription()).isEqualTo(DESCRIPTION);
assertThat(savedVisit.getDate()).isEqualTo(TODAY);
}
}

View file

@ -0,0 +1,38 @@
package org.springframework.samples.petclinic.visit;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
public class VisitTest {
private Visit visit;
@Before
public void setUp() {
visit = new Visit();
}
@Test
public void testGetDate() {
Date today = new Date();
visit.setDate(today);
assertThat(visit.getDate()).isEqualTo(today);
}
@Test
public void testGetDescription() {
String description = "The greatest visit of all time";
visit.setDescription(description);
assertThat(visit.getDescription()).isEqualTo(description);
}
@Test
public void testGetPetId() {
int petId = 1;
visit.setPetId(petId);
assertThat((int) visit.getPetId()).isEqualTo(petId);
}
}