violation = constraintViolations.iterator().next();
+ assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
+ assertThat(violation.getMessage()).isEqualTo("must not be empty");
+ }
}
diff --git a/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java b/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java
index 50758829..1e47a7ef 100644
--- a/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java
+++ b/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java
@@ -15,7 +15,12 @@
*/
package org.springframework.samples.petclinic.service;
-import org.junit.Test;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Collection;
+import java.util.Date;
+
+import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
@@ -25,22 +30,25 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
-import java.util.Collection;
-import java.util.Date;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
/**
- * Base class for {@link ClinicService} integration tests.
Subclasses should specify Spring context
- * configuration using {@link ContextConfiguration @ContextConfiguration} annotation
- * AbstractclinicServiceTests and its subclasses benefit from the following services provided by the Spring
- * TestContext Framework:
- Spring IoC container caching which spares us unnecessary set up
- * time between test execution.
- Dependency Injection of test fixture instances, meaning that
- * we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the
instance variable, which uses autowiring by
- * type. - Transaction management, meaning each test method is executed in its own transaction,
- * which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there
- * is no need for a teardown or cleanup script.
- An {@link org.springframework.context.ApplicationContext
- * ApplicationContext} is also inherited and can be used for explicit bean lookup if necessary.
+ *
+ * Base class for {@link ClinicService} integration tests.
+ *
+ *
+ * Subclasses should specify Spring context configuration using {@link ContextConfiguration @ContextConfiguration} annotation
+ *
+ *
+ * AbstractclinicServiceTests and its subclasses benefit from the following services provided by the Spring TestContext Framework:
+ *
+ *
+ * - Spring IoC container caching which spares us unnecessary set up time between test execution.
+ * - Dependency Injection of test fixture instances, meaning that we don't need to perform application context lookups.
+ * See the use of {@link Autowired @Autowired} on the
instance variable, which uses autowiring by type.
+ * - Transaction management, meaning each test method is executed in its own transaction, which is automatically rolled
+ * back by default. Thus, even if tests insert or otherwise change database state, there is no need for a teardown or cleanup script.
+ *
- An {@link org.springframework.context.ApplicationContext ApplicationContext} is also inherited and can be used for explicit bean
+ * lookup if necessary.
+ *
*
* @author Ken Krebs
* @author Rod Johnson
@@ -50,137 +58,136 @@
*/
public abstract class AbstractClinicServiceTests {
- @Autowired
- protected ClinicService clinicService;
-
- @Test
- public void shouldFindSingleOwnerWithPet() {
- Owner owner = this.clinicService.findOwnerById(1);
- assertThat(owner.getLastName()).startsWith("Franklin");
- assertThat(owner.getPets().size()).isEqualTo(1);
- }
-
- @Test
- public void shouldReturnAllOwnersInCaseLastNameIsEmpty() {
- Collection owners = this.clinicService.findAll();
- assertThat(owners).extracting("lastName").contains("Davis", "Franklin");
- }
-
- @Test
- @Transactional
- public void shouldInsertOwner() {
- Collection owners = this.clinicService.findAll();
- int found = owners.size();
-
- Owner owner = new Owner();
- owner.setFirstName("Sam");
- owner.setLastName("Schultz");
- owner.setAddress("4, Evans Street");
- owner.setCity("Wollongong");
- owner.setTelephone("4444444444");
- this.clinicService.saveOwner(owner);
- assertThat(owner.getId().longValue()).isNotEqualTo(0);
-
- owners = this.clinicService.findAll();
- assertThat(owners.size()).isEqualTo(found + 1);
- }
-
- @Test
- @Transactional
- public void shouldUpdateOwner() {
- Owner owner = this.clinicService.findOwnerById(1);
- String oldLastName = owner.getLastName();
- String newLastName = oldLastName + "X";
-
- owner.setLastName(newLastName);
- this.clinicService.saveOwner(owner);
-
- // retrieving new name from database
- owner = this.clinicService.findOwnerById(1);
- assertThat(owner.getLastName()).isEqualTo(newLastName);
- }
+ @Autowired
+ protected ClinicService clinicService;
+
+ @Test
+ public void shouldFindSingleOwnerWithPet() {
+ Owner owner = clinicService.findOwnerById(1);
+ assertThat(owner.getLastName()).startsWith("Franklin");
+ assertThat(owner.getPets().size()).isEqualTo(1);
+ }
+
+ @Test
+ public void shouldReturnAllOwnersInCaseLastNameIsEmpty() {
+ Collection owners = clinicService.findAll();
+ assertThat(owners).extracting("lastName").contains("Davis", "Franklin");
+ }
+
+ @Test
+ @Transactional
+ public void shouldInsertOwner() {
+ Collection owners = clinicService.findAll();
+ int found = owners.size();
+
+ Owner owner = new Owner();
+ owner.setFirstName("Sam");
+ owner.setLastName("Schultz");
+ owner.setAddress("4, Evans Street");
+ owner.setCity("Wollongong");
+ owner.setTelephone("4444444444");
+ clinicService.saveOwner(owner);
+ assertThat(owner.getId().longValue()).isNotEqualTo(0);
+
+ owners = clinicService.findAll();
+ assertThat(owners.size()).isEqualTo(found + 1);
+ }
+
+ @Test
+ @Transactional
+ public void shouldUpdateOwner() {
+ Owner owner = clinicService.findOwnerById(1);
+ String oldLastName = owner.getLastName();
+ String newLastName = oldLastName + "X";
+
+ owner.setLastName(newLastName);
+ clinicService.saveOwner(owner);
+
+ // retrieving new name from database
+ owner = clinicService.findOwnerById(1);
+ assertThat(owner.getLastName()).isEqualTo(newLastName);
+ }
@Test
public void shouldFindPetWithCorrectId() {
- Pet pet7 = this.clinicService.findPetById(7);
- assertThat(pet7.getName()).startsWith("Samantha");
- assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
-
+ Pet pet7 = clinicService.findPetById(7);
+ assertThat(pet7.getName()).startsWith("Samantha");
+ assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
+
}
@Test
public void shouldFindAllPetTypes() {
- Collection petTypes = this.clinicService.findPetTypes();
-
- PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
- assertThat(petType1.getName()).isEqualTo("cat");
- PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
- assertThat(petType4.getName()).isEqualTo("snake");
+ Collection petTypes = clinicService.findPetTypes();
+
+ PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
+ assertThat(petType1.getName()).isEqualTo("cat");
+ PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
+ assertThat(petType4.getName()).isEqualTo("snake");
}
@Test
@Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() {
- Owner owner6 = this.clinicService.findOwnerById(6);
- int found = owner6.getPets().size();
-
- Pet pet = new Pet();
- pet.setName("bowser");
- Collection types = this.clinicService.findPetTypes();
- pet.setType(EntityUtils.getById(types, PetType.class, 2));
- pet.setBirthDate(new Date());
- owner6.addPet(pet);
- assertThat(owner6.getPets().size()).isEqualTo(found + 1);
-
- this.clinicService.savePet(pet);
- this.clinicService.saveOwner(owner6);
-
- owner6 = this.clinicService.findOwnerById(6);
- assertThat(owner6.getPets().size()).isEqualTo(found + 1);
- // checks that id has been generated
- assertThat(pet.getId()).isNotNull();
+ Owner owner6 = clinicService.findOwnerById(6);
+ int found = owner6.getPets().size();
+
+ Pet pet = new Pet();
+ pet.setName("bowser");
+ Collection types = clinicService.findPetTypes();
+ pet.setType(EntityUtils.getById(types, PetType.class, 2));
+ pet.setBirthDate(new Date());
+ owner6.addPet(pet);
+ assertThat(owner6.getPets().size()).isEqualTo(found + 1);
+
+ clinicService.savePet(pet);
+ clinicService.saveOwner(owner6);
+
+ owner6 = clinicService.findOwnerById(6);
+ assertThat(owner6.getPets().size()).isEqualTo(found + 1);
+ // checks that id has been generated
+ assertThat(pet.getId()).isNotNull();
}
@Test
@Transactional
public void shouldUpdatePetName() throws Exception {
- Pet pet7 = this.clinicService.findPetById(7);
- String oldName = pet7.getName();
-
- String newName = oldName + "X";
+ Pet pet7 = clinicService.findPetById(7);
+ String oldName = pet7.getName();
+
+ String newName = oldName + "X";
pet7.setName(newName);
- this.clinicService.savePet(pet7);
+ clinicService.savePet(pet7);
- pet7 = this.clinicService.findPetById(7);
- assertThat(pet7.getName()).isEqualTo(newName);
+ pet7 = clinicService.findPetById(7);
+ assertThat(pet7.getName()).isEqualTo(newName);
}
@Test
public void shouldFindVets() {
- Collection vets = this.clinicService.findVets();
-
- Vet vet = EntityUtils.getById(vets, Vet.class, 3);
- assertThat(vet.getLastName()).isEqualTo("Douglas");
- assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
- assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
- assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
+ Collection vets = clinicService.findVets();
+
+ Vet vet = EntityUtils.getById(vets, Vet.class, 3);
+ assertThat(vet.getLastName()).isEqualTo("Douglas");
+ assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
+ assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
+ assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
}
@Test
@Transactional
public void shouldAddNewVisitForPet() {
- Pet pet7 = this.clinicService.findPetById(7);
- int found = pet7.getVisits().size();
- Visit visit = new Visit();
- pet7.addVisit(visit);
- visit.setDescription("test");
- this.clinicService.saveVisit(visit);
- this.clinicService.savePet(pet7);
-
- pet7 = this.clinicService.findPetById(7);
- assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
- assertThat(visit.getId()).isNotNull();
+ Pet pet7 = clinicService.findPetById(7);
+ int found = pet7.getVisits().size();
+ Visit visit = new Visit();
+ pet7.addVisit(visit);
+ visit.setDescription("test");
+ clinicService.saveVisit(visit);
+ clinicService.savePet(pet7);
+
+ pet7 = clinicService.findPetById(7);
+ assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
+ assertThat(visit.getId()).isNotNull();
}
-
}
diff --git a/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java b/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java
index 1171b2bc..67e38b60 100644
--- a/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java
+++ b/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java
@@ -1,7 +1,13 @@
package org.springframework.samples.petclinic.web;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -10,55 +16,49 @@
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.service.ClinicService;
-import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
-import static org.mockito.BDDMockito.given;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
-@RunWith(SpringRunner.class)
+@ExtendWith(SpringExtension.class)
@WebMvcTest(PetResource.class)
public class PetResourceTests {
- @Autowired
- private MockMvc mvc;
-
- @MockBean
- ClinicService clinicService;
+ @Autowired
+ private MockMvc mvc;
- @Test
- public void shouldGetAPetInJSonFormat() throws Exception {
+ @MockBean
+ ClinicService clinicService;
- Pet pet = setupPet();
+ @Test
+ public void shouldGetAPetInJSonFormat() throws Exception {
- given(clinicService.findPetById(2)).willReturn(pet);
+ Pet pet = setupPet();
+ given(clinicService.findPetById(2)).willReturn(pet);
- mvc.perform(get("/owners/2/pets/2").accept(MediaType.APPLICATION_JSON))
- .andExpect(status().isOk())
- .andExpect(content().contentType("application/json;charset=UTF-8"))
- .andExpect(jsonPath("$.id").value(2))
- .andExpect(jsonPath("$.name").value("Basil"))
- .andExpect(jsonPath("$.type.id").value(6));
- }
+ mvc.perform(get("/owners/2/pets/2").accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType("application/json"))
+ .andExpect(jsonPath("$.id").value(2))
+ .andExpect(jsonPath("$.name").value("Basil"))
+ .andExpect(jsonPath("$.type.id").value(6));
+ }
- private Pet setupPet() {Owner owner = new Owner();
- owner.setFirstName("George");
- owner.setLastName("Bush");
+ private Pet setupPet() {
+ Owner owner = new Owner();
+ owner.setFirstName("George");
+ owner.setLastName("Bush");
- Pet pet = new Pet();
+ Pet pet = new Pet();
- pet.setName("Basil");
- pet.setId(2);
+ pet.setName("Basil");
+ pet.setId(2);
- PetType petType = new PetType();
- petType.setId(6);
- pet.setType(petType);
+ PetType petType = new PetType();
+ petType.setId(6);
+ pet.setType(petType);
- owner.addPet(pet);
- return pet;
- }
+ owner.addPet(pet);
+ return pet;
+ }
}
diff --git a/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java b/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java
index 895a33ea..4cbe082a 100644
--- a/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java
+++ b/spring-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java
@@ -1,45 +1,44 @@
package org.springframework.samples.petclinic.web;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.service.ClinicService;
-import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
-import java.util.Arrays;
-
-import static org.mockito.BDDMockito.given;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
-@RunWith(SpringRunner.class)
+@ExtendWith(SpringExtension.class)
@WebMvcTest(VetResource.class)
public class VetResourceTests {
- @Autowired
- private MockMvc mvc;
-
- @MockBean
- ClinicService clinicService;
+ @Autowired
+ private MockMvc mvc;
- @Test
- public void shouldGetAListOfVetsInJSonFormat() throws Exception {
+ @MockBean
+ ClinicService clinicService;
- Vet vet = new Vet();
- vet.setId(1);
+ @Test
+ public void shouldGetAListOfVetsInJSonFormat() throws Exception {
- given(clinicService.findVets()).willReturn(Arrays.asList(vet));
+ Vet vet = new Vet();
+ vet.setId(1);
- mvc.perform(get("/vets").accept(MediaType.APPLICATION_JSON))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$[0].id").value(1));
- }
+ given(clinicService.findVets()).willReturn(Arrays.asList(vet));
+ mvc.perform(get("/vets").accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$[0].id").value(1));
+ }
}