|
| 1 | +package org.springframework.samples.petclinic.web; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.test.context.ActiveProfiles; |
| 8 | +import org.springframework.test.context.ContextConfiguration; |
| 9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 10 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 13 | + |
| 14 | +import static org.hamcrest.Matchers.hasProperty; |
| 15 | +import static org.hamcrest.Matchers.is; |
| 16 | + |
| 17 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test class for {@link OwnerController} |
| 25 | + * |
| 26 | + * @author Colin But |
| 27 | + */ |
| 28 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 29 | +@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) |
| 30 | +@WebAppConfiguration |
| 31 | +@ActiveProfiles("spring-data-jpa") |
| 32 | +public class OwnerControllerTests { |
| 33 | + |
| 34 | + private static final int TEST_OWNER_ID = 1; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private OwnerController ownerController; |
| 38 | + |
| 39 | + private MockMvc mockMvc; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setup() { |
| 43 | + this.mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testInitCreationForm() throws Exception { |
| 48 | + mockMvc.perform(get("/owners/new")) |
| 49 | + .andExpect(status().isOk()) |
| 50 | + .andExpect(model().attributeExists("owner")) |
| 51 | + .andExpect(view().name("owners/createOrUpdateOwnerForm")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testProcessCreationFormSuccess() throws Exception { |
| 56 | + mockMvc.perform(post("/owners/new") |
| 57 | + .param("firstName", "Joe") |
| 58 | + .param("lastName", "Bloggs") |
| 59 | + .param("address", "123 Caramel Street") |
| 60 | + .param("city", "London") |
| 61 | + .param("telephone", "01316761638") |
| 62 | + ) |
| 63 | + .andExpect(status().is3xxRedirection()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testProcessCreationFormHasErrors() throws Exception { |
| 68 | + mockMvc.perform(post("/owners/new") |
| 69 | + .param("firstName", "Joe") |
| 70 | + .param("lastName", "Bloggs") |
| 71 | + .param("city", "London") |
| 72 | + ) |
| 73 | + .andExpect(status().isOk()) |
| 74 | + .andExpect(model().attributeHasErrors("owner")) |
| 75 | + .andExpect(model().attributeHasFieldErrors("owner", "address")) |
| 76 | + .andExpect(model().attributeHasFieldErrors("owner", "telephone")) |
| 77 | + .andExpect(view().name("owners/createOrUpdateOwnerForm")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testInitFindForm() throws Exception { |
| 82 | + mockMvc.perform(get("/owners/find")) |
| 83 | + .andExpect(status().isOk()) |
| 84 | + .andExpect(model().attributeExists("owner")) |
| 85 | + .andExpect(view().name("owners/findOwners")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testProcessFindFormSuccess() throws Exception { |
| 90 | + mockMvc.perform(get("/owners")) |
| 91 | + .andExpect(status().isOk()) |
| 92 | + .andExpect(view().name("owners/ownersList")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testProcessFindFormByLastName() throws Exception { |
| 97 | + mockMvc.perform(get("/owners") |
| 98 | + .param("lastName", "Franklin") |
| 99 | + ) |
| 100 | + .andExpect(status().is3xxRedirection()) |
| 101 | + .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testProcessFindFormNoOwnersFound() throws Exception { |
| 106 | + mockMvc.perform(get("/owners") |
| 107 | + .param("lastName", "Unknown Surname") |
| 108 | + ) |
| 109 | + .andExpect(status().isOk()) |
| 110 | + .andExpect(model().attributeHasFieldErrors("owner", "lastName")) |
| 111 | + .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound")) |
| 112 | + .andExpect(view().name("owners/findOwners")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testInitUpdateOwnerForm() throws Exception { |
| 117 | + mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)) |
| 118 | + .andExpect(status().isOk()) |
| 119 | + .andExpect(model().attributeExists("owner")) |
| 120 | + .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) |
| 121 | + .andExpect(model().attribute("owner", hasProperty("firstName", is("George")))) |
| 122 | + .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St.")))) |
| 123 | + .andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) |
| 124 | + .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) |
| 125 | + .andExpect(view().name("owners/createOrUpdateOwnerForm")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testProcessUpdateOwnerFormSuccess() throws Exception { |
| 130 | + mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) |
| 131 | + .param("firstName", "Joe") |
| 132 | + .param("lastName", "Bloggs") |
| 133 | + .param("address", "123 Caramel Street") |
| 134 | + .param("city", "London") |
| 135 | + .param("telephone", "01616291589") |
| 136 | + ) |
| 137 | + .andExpect(status().is3xxRedirection()) |
| 138 | + .andExpect(view().name("redirect:/owners/{ownerId}")); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testProcessUpdateOwnerFormHasErrors() throws Exception { |
| 143 | + mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) |
| 144 | + .param("firstName", "Joe") |
| 145 | + .param("lastName", "Bloggs") |
| 146 | + .param("city", "London") |
| 147 | + ) |
| 148 | + .andExpect(status().isOk()) |
| 149 | + .andExpect(model().attributeHasErrors("owner")) |
| 150 | + .andExpect(model().attributeHasFieldErrors("owner", "address")) |
| 151 | + .andExpect(model().attributeHasFieldErrors("owner", "telephone")) |
| 152 | + .andExpect(view().name("owners/createOrUpdateOwnerForm")); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testShowOwner() throws Exception { |
| 157 | + mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)) |
| 158 | + .andExpect(status().isOk()) |
| 159 | + .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) |
| 160 | + .andExpect(model().attribute("owner", hasProperty("firstName", is("George")))) |
| 161 | + .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St.")))) |
| 162 | + .andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) |
| 163 | + .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) |
| 164 | + .andExpect(view().name("owners/ownerDetails")); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments