package org.springframework.samples.petclinic.web; import org.assertj.core.util.Lists; 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.samples.petclinic.model.Owner; 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.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.mockito.BDDMockito.given; 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.*; /** * Test class for the {@link PetController} * * @author Colin But */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring/mvc-core-config.xml", "classpath:spring/mvc-test-config.xml"}) @WebAppConfiguration 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; @Autowired private ClinicService clinicService; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders .standaloneSetup(petController) .setConversionService(formattingConversionServiceFactoryBean.getObject()) .build(); PetType cat = new PetType(); cat.setId(3); cat.setName("hamster"); given(this.clinicService.findPetTypes()).willReturn(Lists.newArrayList(cat)); given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(new Owner()); given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet()); } @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")); } }