Convert Controler's integration test to unit test

This commit is contained in:
Antoine Rey 2016-08-19 18:29:53 +02:00
parent 1080006218
commit 620141da5d
4 changed files with 85 additions and 45 deletions

View file

@ -1,19 +1,20 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import org.assertj.core.util.Lists;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.samples.petclinic.PetClinicApplication; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles; 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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; 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.hasProperty;
import static org.hamcrest.Matchers.is; 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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 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 * @author Colin But
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = PetClinicApplication.class) @WebMvcTest(OwnerController.class)
@WebAppConfiguration
@ActiveProfiles("test")
public class OwnerControllerTests { public class OwnerControllerTests {
private static final int TEST_OWNER_ID = 1; private static final int TEST_OWNER_ID = 1;
@ -34,11 +33,24 @@ public class OwnerControllerTests {
@Autowired @Autowired
private OwnerController ownerController; private OwnerController ownerController;
@Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean
private ClinicService clinicService;
private Owner george;
@Before @Before
public void setup() { 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 @Test
@ -85,6 +97,7 @@ public class OwnerControllerTests {
@Test @Test
public void testProcessFindFormSuccess() throws Exception { public void testProcessFindFormSuccess() throws Exception {
given(this.clinicService.findOwnerByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
mockMvc.perform(get("/owners")) mockMvc.perform(get("/owners"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(view().name("owners/ownersList")); .andExpect(view().name("owners/ownersList"));
@ -92,6 +105,7 @@ public class OwnerControllerTests {
@Test @Test
public void testProcessFindFormByLastName() throws Exception { public void testProcessFindFormByLastName() throws Exception {
given(this.clinicService.findOwnerByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
mockMvc.perform(get("/owners") mockMvc.perform(get("/owners")
.param("lastName", "Franklin") .param("lastName", "Franklin")
) )

View file

@ -1,18 +1,22 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import org.assertj.core.util.Lists;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.format.support.DefaultFormattingConversionService; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.samples.petclinic.PetClinicApplication; import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles; 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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; 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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 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 * @author Colin But
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = PetClinicApplication.class) @WebMvcTest(value = PetController.class,
@WebAppConfiguration includeFilters = @ComponentScan.Filter(
@ActiveProfiles("test") value = PetTypeFormatter.class,
type = FilterType.ASSIGNABLE_TYPE))
public class PetControllerTests { public class PetControllerTests {
private static final int TEST_OWNER_ID = 1; private static final int TEST_OWNER_ID = 1;
@ -35,18 +40,20 @@ public class PetControllerTests {
private PetController petController; private PetController petController;
@Autowired @Autowired
private PetTypeFormatter petTypeFormatter;
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean
private ClinicService clinicService;
@Before @Before
public void setup() { public void setup() {
DefaultFormattingConversionService formattingConversionService = new DefaultFormattingConversionService(); PetType cat = new PetType();
formattingConversionService.addFormatter(petTypeFormatter); cat.setId(3);
this.mockMvc = MockMvcBuilders cat.setName("hamster");
.standaloneSetup(petController) given(this.clinicService.findPetTypes()).willReturn(Lists.newArrayList(cat));
.setConversionService(formattingConversionService) given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(new Owner());
.build(); given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet());
} }
@Test @Test

View file

@ -1,20 +1,22 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import org.assertj.core.util.Lists;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.http.MediaType;
import org.springframework.samples.petclinic.PetClinicApplication; import org.springframework.samples.petclinic.model.Specialty;
import org.springframework.test.context.ActiveProfiles; 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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions; 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.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.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 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} * Test class for the {@link VetController}
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = PetClinicApplication.class) @WebMvcTest(VetController.class)
@WebAppConfiguration
@ActiveProfiles("test")
public class VetControllerTests { public class VetControllerTests {
@Autowired @Autowired
private VetController vetController; private VetController vetController;
@Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean
private ClinicService clinicService;
@Before @Before
public void setup() { 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 @Test

View file

@ -4,14 +4,14 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.samples.petclinic.PetClinicApplication; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles; 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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; 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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 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 * @author Colin But
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = PetClinicApplication.class) @WebMvcTest(VisitController.class)
@WebAppConfiguration
@ActiveProfiles("test")
public class VisitControllerTests { public class VisitControllerTests {
private static final int TEST_PET_ID = 1; private static final int TEST_PET_ID = 1;
@ -32,13 +30,18 @@ public class VisitControllerTests {
@Autowired @Autowired
private VisitController visitController; private VisitController visitController;
@Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean
private ClinicService clinicService;
@Before @Before
public void setup() { public void init() {
this.mockMvc = MockMvcBuilders.standaloneSetup(visitController).build(); given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet());
} }
@Test @Test
public void testInitNewVisitForm() throws Exception { public void testInitNewVisitForm() throws Exception {
mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)) mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))