From 620141da5d64dab530b06fdd200383c225f50fcf Mon Sep 17 00:00:00 2001 From: Antoine Rey <antoine.rey@gmail.com> Date: Fri, 19 Aug 2016 18:29:53 +0200 Subject: [PATCH] Convert Controler's integration test to unit test --- .../petclinic/web/OwnerControllerTests.java | 32 +++++++++++---- .../petclinic/web/PetControllerTests.java | 41 +++++++++++-------- .../petclinic/web/VetControllerTests.java | 34 +++++++++++---- .../petclinic/web/VisitControllerTests.java | 23 ++++++----- 4 files changed, 85 insertions(+), 45 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java index 36f7d80..e3def7e 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java @@ -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") ) diff --git a/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java index 060ef0e..a5502a3 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java @@ -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 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 e03c948..056105e 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java @@ -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 diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java index dbb5069..1793015 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java @@ -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)) -- GitLab