diff --git a/pom.xml b/pom.xml
index fdd16f409..5e555e9c5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,6 +26,8 @@
2.2.0
+ 1.10.19
+ 1.3
1.3
@@ -257,6 +259,17 @@
${assertj.version}
test
+
+ org.mockito
+ mockito-all
+ ${mockito.version}
+ test
+
+
+ org.hamcrest
+ hamcrest-all
+ ${hamcrest.version}
+
diff --git a/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java
new file mode 100644
index 000000000..ee83b8a4d
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java
@@ -0,0 +1,53 @@
+package org.springframework.samples.petclinic.web;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
+
+/**
+ * Test class for {@link CrashController}
+ *
+ * @author Colin But
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
+@WebAppConfiguration
+@ActiveProfiles("spring-data-jpa")
+public class CrashControllerTests {
+
+ @Autowired
+ private CrashController crashController;
+
+ @Autowired
+ private SimpleMappingExceptionResolver simpleMappingExceptionResolver;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ this.mockMvc = MockMvcBuilders
+ .standaloneSetup(crashController)
+ .setHandlerExceptionResolvers(simpleMappingExceptionResolver)
+ .build();
+ }
+
+ @Test
+ public void testTriggerException() throws Exception {
+ mockMvc.perform(get("/oups"))
+ .andExpect(view().name("exception"))
+ .andExpect(model().attributeExists("exception"))
+ .andExpect(forwardedUrl("exception"))
+ .andExpect(status().isOk());
+ }
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java
new file mode 100644
index 000000000..2d85c9d15
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java
@@ -0,0 +1,167 @@
+package org.springframework.samples.petclinic.web;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.hamcrest.Matchers.hasProperty;
+import static org.hamcrest.Matchers.is;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+/**
+ * Test class for {@link OwnerController}
+ *
+ * @author Colin But
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
+@WebAppConfiguration
+@ActiveProfiles("spring-data-jpa")
+public class OwnerControllerTests {
+
+ private static final int TEST_OWNER_ID = 1;
+
+ @Autowired
+ private OwnerController ownerController;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ this.mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build();
+ }
+
+ @Test
+ public void testInitCreationForm() throws Exception {
+ mockMvc.perform(get("/owners/new"))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeExists("owner"))
+ .andExpect(view().name("owners/createOrUpdateOwnerForm"));
+ }
+
+ @Test
+ public void testProcessCreationFormSuccess() throws Exception {
+ mockMvc.perform(post("/owners/new")
+ .param("firstName", "Joe")
+ .param("lastName", "Bloggs")
+ .param("address", "123 Caramel Street")
+ .param("city", "London")
+ .param("telephone", "01316761638")
+ )
+ .andExpect(status().is3xxRedirection());
+ }
+
+ @Test
+ public void testProcessCreationFormHasErrors() throws Exception {
+ mockMvc.perform(post("/owners/new")
+ .param("firstName", "Joe")
+ .param("lastName", "Bloggs")
+ .param("city", "London")
+ )
+ .andExpect(status().isOk())
+ .andExpect(model().attributeHasErrors("owner"))
+ .andExpect(model().attributeHasFieldErrors("owner", "address"))
+ .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
+ .andExpect(view().name("owners/createOrUpdateOwnerForm"));
+ }
+
+ @Test
+ public void testInitFindForm() throws Exception {
+ mockMvc.perform(get("/owners/find"))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeExists("owner"))
+ .andExpect(view().name("owners/findOwners"));
+ }
+
+ @Test
+ public void testProcessFindFormSuccess() throws Exception {
+ mockMvc.perform(get("/owners"))
+ .andExpect(status().isOk())
+ .andExpect(view().name("owners/ownersList"));
+ }
+
+ @Test
+ public void testProcessFindFormByLastName() throws Exception {
+ mockMvc.perform(get("/owners")
+ .param("lastName", "Franklin")
+ )
+ .andExpect(status().is3xxRedirection())
+ .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
+ }
+
+ @Test
+ public void testProcessFindFormNoOwnersFound() throws Exception {
+ mockMvc.perform(get("/owners")
+ .param("lastName", "Unknown Surname")
+ )
+ .andExpect(status().isOk())
+ .andExpect(model().attributeHasFieldErrors("owner", "lastName"))
+ .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
+ .andExpect(view().name("owners/findOwners"));
+ }
+
+ @Test
+ public void testInitUpdateOwnerForm() throws Exception {
+ mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeExists("owner"))
+ .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
+ .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
+ .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
+ .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
+ .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
+ .andExpect(view().name("owners/createOrUpdateOwnerForm"));
+ }
+
+ @Test
+ public void testProcessUpdateOwnerFormSuccess() throws Exception {
+ mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
+ .param("firstName", "Joe")
+ .param("lastName", "Bloggs")
+ .param("address", "123 Caramel Street")
+ .param("city", "London")
+ .param("telephone", "01616291589")
+ )
+ .andExpect(status().is3xxRedirection())
+ .andExpect(view().name("redirect:/owners/{ownerId}"));
+ }
+
+ @Test
+ public void testProcessUpdateOwnerFormHasErrors() throws Exception {
+ mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
+ .param("firstName", "Joe")
+ .param("lastName", "Bloggs")
+ .param("city", "London")
+ )
+ .andExpect(status().isOk())
+ .andExpect(model().attributeHasErrors("owner"))
+ .andExpect(model().attributeHasFieldErrors("owner", "address"))
+ .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
+ .andExpect(view().name("owners/createOrUpdateOwnerForm"));
+ }
+
+ @Test
+ public void testShowOwner() throws Exception {
+ mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))
+ .andExpect(status().isOk())
+ .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
+ .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
+ .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
+ .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
+ .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
+ .andExpect(view().name("owners/ownerDetails"));
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java
new file mode 100644
index 000000000..8d42c7731
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java
@@ -0,0 +1,113 @@
+package org.springframework.samples.petclinic.web;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.format.support.FormattingConversionServiceFactoryBean;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+/**
+ * Test class for the {@link PetController}
+ *
+ * @author Colin But
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
+@WebAppConfiguration
+@ActiveProfiles("spring-data-jpa")
+public class PetControllerTests {
+
+ private static final int TEST_OWNER_ID = 1;
+ private static final int TEST_PET_ID = 1;
+
+ @Autowired
+ private PetController petController;
+
+ @Autowired
+ private FormattingConversionServiceFactoryBean formattingConversionServiceFactoryBean;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ this.mockMvc = MockMvcBuilders
+ .standaloneSetup(petController)
+ .setConversionService(formattingConversionServiceFactoryBean.getObject())
+ .build();
+ }
+
+ @Test
+ public void testInitCreationForm() throws Exception {
+ mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
+ .andExpect(status().isOk())
+ .andExpect(view().name("pets/createOrUpdatePetForm"))
+ .andExpect(model().attributeExists("pet"));
+ }
+
+ @Test
+ public void testProcessCreationFormSuccess() throws Exception {
+ mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
+ .param("name", "Betty")
+ .param("type", "hamster")
+ .param("birthDate", "2015/02/12")
+ )
+ .andExpect(status().is3xxRedirection())
+ .andExpect(view().name("redirect:/owners/{ownerId}"));
+ }
+
+ @Test
+ public void testProcessCreationFormHasErrors() throws Exception {
+ mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
+ .param("name", "Betty")
+ .param("birthDate", "2015/02/12")
+ )
+ .andExpect(model().attributeHasNoErrors("owner"))
+ .andExpect(model().attributeHasErrors("pet"))
+ .andExpect(status().isOk())
+ .andExpect(view().name("pets/createOrUpdatePetForm"));
+ }
+
+ @Test
+ public void testInitUpdateForm() throws Exception {
+ mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeExists("pet"))
+ .andExpect(view().name("pets/createOrUpdatePetForm"));
+ }
+
+ @Test
+ public void testProcessUpdateFormSuccess() throws Exception {
+ mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
+ .param("name", "Betty")
+ .param("type", "hamster")
+ .param("birthDate", "2015/02/12")
+ )
+ .andExpect(status().is3xxRedirection())
+ .andExpect(view().name("redirect:/owners/{ownerId}"));
+ }
+
+ @Test
+ public void testProcessUpdateFormHasErrors() throws Exception {
+ mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
+ .param("name", "Betty")
+ .param("birthDate", "2015/02/12")
+ )
+ .andExpect(model().attributeHasNoErrors("owner"))
+ .andExpect(model().attributeHasErrors("pet"))
+ .andExpect(status().isOk())
+ .andExpect(view().name("pets/createOrUpdatePetForm"));
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/web/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/web/PetTypeFormatterTests.java
new file mode 100644
index 000000000..e1b2a53fb
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/web/PetTypeFormatterTests.java
@@ -0,0 +1,76 @@
+package org.springframework.samples.petclinic.web;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.springframework.samples.petclinic.model.PetType;
+import org.springframework.samples.petclinic.service.ClinicService;
+
+import java.text.ParseException;
+import java.util.*;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test class for {@link PetTypeFormatter}
+ *
+ * @author Colin But
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class PetTypeFormatterTests {
+
+ @Mock
+ private ClinicService clinicService;
+
+ private PetTypeFormatter petTypeFormatter;
+
+ @Before
+ public void setup() {
+ petTypeFormatter = new PetTypeFormatter(clinicService);
+ }
+
+ @Test
+ public void testPrint() {
+ PetType petType = new PetType();
+ petType.setName("Hamster");
+ String petTypeName = petTypeFormatter.print(petType, Locale.ENGLISH);
+ assertEquals("Hamster", petTypeName);
+ }
+
+ @Test
+ public void shouldParse() throws ParseException {
+ Mockito.when(clinicService.findPetTypes()).thenReturn(makePetTypes());
+ PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
+ assertEquals("Bird", petType.getName());
+ }
+
+ @Test(expected = ParseException.class)
+ public void shouldThrowParseException() throws ParseException {
+ Mockito.when(clinicService.findPetTypes()).thenReturn(makePetTypes());
+ petTypeFormatter.parse("Fish", Locale.ENGLISH);
+ }
+
+ /**
+ * Helper method to produce some sample pet types just for test purpose
+ *
+ * @return {@link Collection} of {@link PetType}
+ */
+ private Collection makePetTypes() {
+ Collection petTypes = new ArrayList<>();
+ petTypes.add(new PetType(){
+ {
+ setName("Dog");
+ }
+ });
+ petTypes.add(new PetType(){
+ {
+ setName("Bird");
+ }
+ });
+ return petTypes;
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java
index 28b9348a1..5446c0718 100644
--- a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java
@@ -1,9 +1,7 @@
package org.springframework.samples.petclinic.web;
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 static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.Before;
import org.junit.Test;
@@ -17,10 +15,9 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
-import org.springframework.web.context.WebApplicationContext;
/**
- * Test class for the UserResource REST controller.
+ * Test class for the {@link VetController}
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
@@ -39,10 +36,29 @@ public class VetControllerTests {
}
@Test
- public void testGetExistingUser() throws Exception {
+ public void testShowVetListXml() throws Exception {
+ testShowVetList("/vets.xml");
+ }
+
+ @Test
+ public void testShowVetListHtml() throws Exception {
+ testShowVetList("/vets.html");
+ }
+
+ @Test
+ public void testShowResourcesVetList() throws Exception {
ResultActions actions = mockMvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
actions.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.vetList[0].id").value(1));
}
+
+ private void testShowVetList(String url) throws Exception {
+ mockMvc.perform(get(url))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeExists("vets"))
+ .andExpect(view().name("vets/vetList"));
+ }
+
+
}
diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java
new file mode 100644
index 000000000..a9f9ea045
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java
@@ -0,0 +1,79 @@
+package org.springframework.samples.petclinic.web;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+/**
+ * Test class for {@link VisitController}
+ *
+ * @author Colin But
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
+@WebAppConfiguration
+@ActiveProfiles("spring-data-jpa")
+public class VisitControllerTests {
+
+ private static final int TEST_PET_ID = 1;
+
+ @Autowired
+ private VisitController visitController;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ this.mockMvc = MockMvcBuilders.standaloneSetup(visitController).build();
+ }
+
+ @Test
+ public void testInitNewVisitForm() throws Exception {
+ mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))
+ .andExpect(status().isOk())
+ .andExpect(view().name("pets/createOrUpdateVisitForm"));
+ }
+
+ @Test
+ public void testProcessNewVisitFormSuccess() throws Exception {
+ mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)
+ .param("name", "George")
+ .param("description", "Visit Description")
+ )
+ .andExpect(status().is3xxRedirection())
+ .andExpect(view().name("redirect:/owners/{ownerId}"));
+ }
+
+ @Test
+ public void testProcessNewVisitFormHasErrors() throws Exception {
+ mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)
+ .param("name", "George")
+ )
+ .andExpect(model().attributeHasErrors("visit"))
+ .andExpect(status().isOk())
+ .andExpect(view().name("pets/createOrUpdateVisitForm"));
+ }
+
+ @Test
+ public void testShowVisits() throws Exception {
+ mockMvc.perform(get("/owners/*/pets/{petId}/visits", TEST_PET_ID))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeExists("visits"))
+ .andExpect(view().name("visitList"));
+ }
+
+
+}