mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:35:49 +00:00
Migrated test assertions to AssertJ
This commit is contained in:
parent
35a179f88b
commit
1dfc3b7a3a
4 changed files with 42 additions and 42 deletions
10
pom.xml
10
pom.xml
|
@ -49,7 +49,7 @@
|
||||||
|
|
||||||
<!-- Test -->
|
<!-- Test -->
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<hamcrest.version>1.3</hamcrest.version>
|
<assertj.version>1.7.1</assertj.version>
|
||||||
|
|
||||||
<!-- Dates -->
|
<!-- Dates -->
|
||||||
<jodatime-hibernate.version>1.3</jodatime-hibernate.version>
|
<jodatime-hibernate.version>1.3</jodatime-hibernate.version>
|
||||||
|
@ -278,12 +278,10 @@
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- used by Spring MVC Test framework -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hamcrest</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>hamcrest-library</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${hamcrest.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package org.springframework.samples.petclinic.model;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.validation.ConstraintViolation;
|
import javax.validation.ConstraintViolation;
|
||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
@ -39,8 +42,8 @@ public class ValidatorTests {
|
||||||
|
|
||||||
Assert.assertEquals(1, constraintViolations.size());
|
Assert.assertEquals(1, constraintViolations.size());
|
||||||
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
||||||
Assert.assertEquals(violation.getPropertyPath().toString(), "firstName");
|
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
|
||||||
Assert.assertEquals(violation.getMessage(), "may not be empty");
|
assertThat(violation.getMessage()).isEqualTo("may not be empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.service;
|
package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@ -61,19 +58,19 @@ public abstract class AbstractClinicServiceTests {
|
||||||
@Test
|
@Test
|
||||||
public void shouldFindOwners() {
|
public void shouldFindOwners() {
|
||||||
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis");
|
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis");
|
||||||
assertEquals(2, owners.size());
|
assertThat(owners.size()).isEqualTo(2);
|
||||||
owners = this.clinicService.findOwnerByLastName("Daviss");
|
owners = this.clinicService.findOwnerByLastName("Daviss");
|
||||||
assertEquals(0, owners.size());
|
assertThat(owners.size()).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFindSingleOwner() {
|
public void shouldFindSingleOwner() {
|
||||||
Owner owner1 = this.clinicService.findOwnerById(1);
|
Owner owner1 = this.clinicService.findOwnerById(1);
|
||||||
assertTrue(owner1.getLastName().startsWith("Franklin"));
|
assertThat(owner1.getLastName()).startsWith("Franklin");
|
||||||
|
|
||||||
Owner owner10 = this.clinicService.findOwnerById(10);
|
Owner owner10 = this.clinicService.findOwnerById(10);
|
||||||
assertEquals("Carlos", owner10.getFirstName());
|
assertThat(owner10.getFirstName()).isEqualTo("Carlos");
|
||||||
assertEquals(owner1.getPets().size(), 1);
|
assertThat(owner1.getPets().size()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -88,10 +85,10 @@ public abstract class AbstractClinicServiceTests {
|
||||||
owner.setCity("Wollongong");
|
owner.setCity("Wollongong");
|
||||||
owner.setTelephone("4444444444");
|
owner.setTelephone("4444444444");
|
||||||
this.clinicService.saveOwner(owner);
|
this.clinicService.saveOwner(owner);
|
||||||
assertNotEquals("Owner Id should have been generated", owner.getId().longValue(), 0);
|
assertThat(owner.getId().longValue()).isNotEqualTo(0);
|
||||||
|
|
||||||
owners = this.clinicService.findOwnerByLastName("Schultz");
|
owners = this.clinicService.findOwnerByLastName("Schultz");
|
||||||
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
|
assertThat(owners.size()).isEqualTo(found + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -103,20 +100,23 @@ public abstract class AbstractClinicServiceTests {
|
||||||
this.clinicService.saveOwner(o1);
|
this.clinicService.saveOwner(o1);
|
||||||
o1 = this.clinicService.findOwnerById(1);
|
o1 = this.clinicService.findOwnerById(1);
|
||||||
|
|
||||||
assertEquals(old + "X", o1.getLastName());
|
assertThat(o1.getLastName()).isEqualTo(old + "X");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFindPetWithCorrectId() {
|
public void shouldFindPetWithCorrectId() {
|
||||||
Collection<PetType> types = this.clinicService.findPetTypes();
|
Collection<PetType> types = this.clinicService.findPetTypes();
|
||||||
|
|
||||||
Pet pet7 = this.clinicService.findPetById(7);
|
Pet pet7 = this.clinicService.findPetById(7);
|
||||||
assertTrue(pet7.getName().startsWith("Samantha"));
|
assertThat(pet7.getName()).startsWith("Samantha");
|
||||||
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId());
|
assertThat(EntityUtils.getById(types, PetType.class, 1).getId()).isEqualTo(pet7.getType().getId());
|
||||||
assertEquals("Jean", pet7.getOwner().getFirstName());
|
assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
|
||||||
|
|
||||||
Pet pet6 = this.clinicService.findPetById(6);
|
Pet pet6 = this.clinicService.findPetById(6);
|
||||||
assertEquals("George", pet6.getName());
|
assertThat(pet6.getName()).isEqualTo("George");
|
||||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId());
|
|
||||||
assertEquals("Peter", pet6.getOwner().getFirstName());
|
assertThat(EntityUtils.getById(types, PetType.class, 4).getId()).isEqualTo(pet6.getType().getId());
|
||||||
|
assertThat(pet6.getOwner().getFirstName()).isEqualTo("Peter");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -124,9 +124,9 @@ public abstract class AbstractClinicServiceTests {
|
||||||
Collection<PetType> petTypes = this.clinicService.findPetTypes();
|
Collection<PetType> petTypes = this.clinicService.findPetTypes();
|
||||||
|
|
||||||
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||||
assertEquals("cat", petType1.getName());
|
assertThat(petType1.getName()).isEqualTo("cat");
|
||||||
PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
|
PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
|
||||||
assertEquals("snake", petType4.getName());
|
assertThat(petType4.getName()).isEqualTo("snake");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -140,13 +140,13 @@ public abstract class AbstractClinicServiceTests {
|
||||||
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
||||||
pet.setBirthDate(new DateTime());
|
pet.setBirthDate(new DateTime());
|
||||||
owner6.addPet(pet);
|
owner6.addPet(pet);
|
||||||
assertEquals(found + 1, owner6.getPets().size());
|
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
|
||||||
// both storePet and storeOwner are necessary to cover all ORM tools
|
// both storePet and storeOwner are necessary to cover all ORM tools
|
||||||
this.clinicService.savePet(pet);
|
this.clinicService.savePet(pet);
|
||||||
this.clinicService.saveOwner(owner6);
|
this.clinicService.saveOwner(owner6);
|
||||||
owner6 = this.clinicService.findOwnerById(6);
|
owner6 = this.clinicService.findOwnerById(6);
|
||||||
assertEquals(found + 1, owner6.getPets().size());
|
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
|
||||||
assertNotNull("Pet Id should have been generated", pet.getId());
|
assertThat(pet.getId()).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -157,7 +157,7 @@ public abstract class AbstractClinicServiceTests {
|
||||||
pet7.setName(old + "X");
|
pet7.setName(old + "X");
|
||||||
this.clinicService.savePet(pet7);
|
this.clinicService.savePet(pet7);
|
||||||
pet7 = this.clinicService.findPetById(7);
|
pet7 = this.clinicService.findPetById(7);
|
||||||
assertEquals(old + "X", pet7.getName());
|
assertThat(pet7.getName()).isEqualTo(old + "X");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -165,14 +165,14 @@ public abstract class AbstractClinicServiceTests {
|
||||||
Collection<Vet> vets = this.clinicService.findVets();
|
Collection<Vet> vets = this.clinicService.findVets();
|
||||||
|
|
||||||
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
|
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
|
||||||
assertEquals("Leary", v1.getLastName());
|
assertThat(v1.getLastName()).isEqualTo("Leary");
|
||||||
assertEquals(1, v1.getNrOfSpecialties());
|
assertThat(v1.getNrOfSpecialties()).isEqualTo(1);
|
||||||
assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
|
assertThat(v1.getSpecialties().get(0).getName()).isEqualTo("radiology");
|
||||||
Vet v2 = EntityUtils.getById(vets, Vet.class, 3);
|
Vet v2 = EntityUtils.getById(vets, Vet.class, 3);
|
||||||
assertEquals("Douglas", v2.getLastName());
|
assertThat(v2.getLastName()).isEqualTo("Douglas");
|
||||||
assertEquals(2, v2.getNrOfSpecialties());
|
assertThat(v2.getNrOfSpecialties()).isEqualTo(2);
|
||||||
assertEquals("dentistry", (v2.getSpecialties().get(0)).getName());
|
assertThat(v2.getSpecialties().get(0).getName()).isEqualTo("dentistry");
|
||||||
assertEquals("surgery", (v2.getSpecialties().get(1)).getName());
|
assertThat(v2.getSpecialties().get(1).getName()).isEqualTo("surgery");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -187,8 +187,8 @@ public abstract class AbstractClinicServiceTests {
|
||||||
this.clinicService.saveVisit(visit);
|
this.clinicService.saveVisit(visit);
|
||||||
this.clinicService.savePet(pet7);
|
this.clinicService.savePet(pet7);
|
||||||
pet7 = this.clinicService.findPetById(7);
|
pet7 = this.clinicService.findPetById(7);
|
||||||
assertEquals(found + 1, pet7.getVisits().size());
|
assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
|
||||||
assertNotNull("Visit Id should have been generated", visit.getId());
|
assertThat(visit.getId()).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.web;
|
package org.springframework.samples.petclinic.web;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
|
Loading…
Reference in a new issue