mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:35:49 +00:00
Convert Controler's integration test to unit test
This commit is contained in:
parent
1080006218
commit
620141da5d
4 changed files with 85 additions and 45 deletions
|
@ -1,19 +1,20 @@
|
|||
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.boot.test.context.SpringBootTest;
|
||||
import org.springframework.samples.petclinic.PetClinicApplication;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
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.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.*;
|
||||
|
@ -24,9 +25,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
* @author Colin But
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = PetClinicApplication.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
@WebMvcTest(OwnerController.class)
|
||||
public class OwnerControllerTests {
|
||||
|
||||
private static final int TEST_OWNER_ID = 1;
|
||||
|
@ -34,11 +33,24 @@ public class OwnerControllerTests {
|
|||
@Autowired
|
||||
private OwnerController ownerController;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private ClinicService clinicService;
|
||||
|
||||
private Owner george;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build();
|
||||
george = new Owner();
|
||||
george.setId(TEST_OWNER_ID);
|
||||
george.setFirstName("George");
|
||||
george.setLastName("Franklin");
|
||||
george.setAddress("110 W. Liberty St.");
|
||||
george.setCity("Madison");
|
||||
george.setTelephone("6085551023");
|
||||
given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(george);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -85,6 +97,7 @@ public class OwnerControllerTests {
|
|||
|
||||
@Test
|
||||
public void testProcessFindFormSuccess() throws Exception {
|
||||
given(this.clinicService.findOwnerByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
|
||||
mockMvc.perform(get("/owners"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("owners/ownersList"));
|
||||
|
@ -92,6 +105,7 @@ public class OwnerControllerTests {
|
|||
|
||||
@Test
|
||||
public void testProcessFindFormByLastName() throws Exception {
|
||||
given(this.clinicService.findOwnerByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
|
||||
mockMvc.perform(get("/owners")
|
||||
.param("lastName", "Franklin")
|
||||
)
|
||||
|
|
|
@ -1,18 +1,22 @@
|
|||
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.boot.test.context.SpringBootTest;
|
||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
||||
import org.springframework.samples.petclinic.PetClinicApplication;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
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.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.*;
|
||||
|
@ -23,9 +27,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
* @author Colin But
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = PetClinicApplication.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
@WebMvcTest(value = PetController.class,
|
||||
includeFilters = @ComponentScan.Filter(
|
||||
value = PetTypeFormatter.class,
|
||||
type = FilterType.ASSIGNABLE_TYPE))
|
||||
public class PetControllerTests {
|
||||
|
||||
private static final int TEST_OWNER_ID = 1;
|
||||
|
@ -35,18 +40,20 @@ public class PetControllerTests {
|
|||
private PetController petController;
|
||||
|
||||
@Autowired
|
||||
private PetTypeFormatter petTypeFormatter;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private ClinicService clinicService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
DefaultFormattingConversionService formattingConversionService = new DefaultFormattingConversionService();
|
||||
formattingConversionService.addFormatter(petTypeFormatter);
|
||||
this.mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(petController)
|
||||
.setConversionService(formattingConversionService)
|
||||
.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
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
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.boot.test.context.SpringBootTest;
|
||||
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.PetClinicApplication;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.samples.petclinic.model.Specialty;
|
||||
import org.springframework.samples.petclinic.model.Vet;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
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.ResultActions;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.hamcrest.xml.HasXPath.hasXPath;
|
||||
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.*;
|
||||
|
||||
|
@ -22,19 +24,33 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
* Test class for the {@link VetController}
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = PetClinicApplication.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
@WebMvcTest(VetController.class)
|
||||
public class VetControllerTests {
|
||||
|
||||
@Autowired
|
||||
private VetController vetController;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private ClinicService clinicService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(vetController).build();
|
||||
Vet james = new Vet();
|
||||
james.setFirstName("James");
|
||||
james.setLastName("Carter");
|
||||
james.setId(1);
|
||||
Vet helen = new Vet();
|
||||
helen.setFirstName("Helen");
|
||||
helen.setLastName("Leary");
|
||||
helen.setId(2);
|
||||
Specialty radiology = new Specialty();
|
||||
radiology.setId(1);
|
||||
radiology.setName("radiology");
|
||||
helen.addSpecialty(radiology);
|
||||
given(this.clinicService.findVets()).willReturn(Lists.newArrayList(james, helen));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -4,14 +4,14 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.samples.petclinic.PetClinicApplication;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
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.*;
|
||||
|
@ -22,9 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
* @author Colin But
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = PetClinicApplication.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
@WebMvcTest(VisitController.class)
|
||||
public class VisitControllerTests {
|
||||
|
||||
private static final int TEST_PET_ID = 1;
|
||||
|
@ -32,13 +30,18 @@ public class VisitControllerTests {
|
|||
@Autowired
|
||||
private VisitController visitController;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private ClinicService clinicService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(visitController).build();
|
||||
public void init() {
|
||||
given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInitNewVisitForm() throws Exception {
|
||||
mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))
|
||||
|
|
Loading…
Reference in a new issue