From e5254156eca3a8461fa758f17dc5fae27e738ab5 Mon Sep 17 00:00:00 2001 From: Antoine Rey <antoine.rey@gmail.com> Date: Fri, 19 Aug 2016 18:54:56 +0200 Subject: [PATCH] Convert Controler's integration test to unit test --- .../petclinic/web/CrashControllerTests.java | 9 +++--- .../petclinic/web/OwnerControllerTests.java | 32 +++++++++++++++---- .../petclinic/web/PetControllerTests.java | 30 +++++++++++------ .../petclinic/web/VetControllerTests.java | 26 +++++++++++++-- .../petclinic/web/VisitControllerTests.java | 16 ++++++---- src/test/resources/spring/mvc-test-config.xml | 12 +++++++ 6 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 src/test/resources/spring/mvc-test-config.xml diff --git a/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java index ee83b8a..a83255b 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java @@ -1,8 +1,5 @@ package org.springframework.samples.petclinic.web; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -15,15 +12,17 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + /** * Test class for {@link CrashController} * * @author Colin But */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@ContextConfiguration({"classpath:spring/mvc-core-config.xml", "classpath:spring/mvc-test-config.xml"}) @WebAppConfiguration -@ActiveProfiles("spring-data-jpa") public class CrashControllerTests { @Autowired 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 2d85c9d..abf0885 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java @@ -1,10 +1,12 @@ 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.test.context.ActiveProfiles; +import org.springframework.samples.petclinic.model.Owner; +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; @@ -13,12 +15,10 @@ 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.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** * Test class for {@link OwnerController} @@ -26,9 +26,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * @author Colin But */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@ContextConfiguration({"classpath:spring/mvc-test-config.xml", "classpath:spring/mvc-core-config.xml"}) @WebAppConfiguration -@ActiveProfiles("spring-data-jpa") public class OwnerControllerTests { private static final int TEST_OWNER_ID = 1; @@ -36,11 +35,26 @@ public class OwnerControllerTests { @Autowired private OwnerController ownerController; + @Autowired + private ClinicService clinicService; + private MockMvc mockMvc; + 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 @@ -87,6 +101,8 @@ 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")); @@ -94,6 +110,8 @@ 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 8d42c77..d875135 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java @@ -1,32 +1,34 @@ package org.springframework.samples.petclinic.web; -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.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; - +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.test.context.ActiveProfiles; +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/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@ContextConfiguration({"classpath:spring/mvc-core-config.xml", "classpath:spring/mvc-test-config.xml"}) @WebAppConfiguration -@ActiveProfiles("spring-data-jpa") public class PetControllerTests { private static final int TEST_OWNER_ID = 1; @@ -38,6 +40,9 @@ public class PetControllerTests { @Autowired private FormattingConversionServiceFactoryBean formattingConversionServiceFactoryBean; + @Autowired + private ClinicService clinicService; + private MockMvc mockMvc; @Before @@ -46,6 +51,13 @@ public class PetControllerTests { .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 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 8b76fd2..b258b8d 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java @@ -1,11 +1,14 @@ 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.http.MediaType; -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.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -14,6 +17,7 @@ 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.*; @@ -21,19 +25,35 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * Test class for the {@link VetController} */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@ContextConfiguration({"classpath:spring/mvc-core-config.xml", "classpath:spring/mvc-test-config.xml"}) @WebAppConfiguration -@ActiveProfiles("spring-data-jpa") public class VetControllerTests { @Autowired private VetController vetController; + @Autowired + private ClinicService clinicService; + private MockMvc mockMvc; @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 a9f9ea0..25905df 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java @@ -4,18 +4,18 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -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.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.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** * Test class for {@link VisitController} @@ -23,9 +23,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * @author Colin But */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@ContextConfiguration({"classpath:spring/mvc-core-config.xml", "classpath:spring/mvc-test-config.xml"}) @WebAppConfiguration -@ActiveProfiles("spring-data-jpa") public class VisitControllerTests { private static final int TEST_PET_ID = 1; @@ -33,11 +32,16 @@ public class VisitControllerTests { @Autowired private VisitController visitController; + @Autowired + private ClinicService clinicService; + private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.standaloneSetup(visitController).build(); + + given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet()); } @Test diff --git a/src/test/resources/spring/mvc-test-config.xml b/src/test/resources/spring/mvc-test-config.xml new file mode 100644 index 0000000..ab3b93b --- /dev/null +++ b/src/test/resources/spring/mvc-test-config.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://www.springframework.org/schema/beans" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd"> + + + <bean class="org.mockito.Mockito" factory-method="mock"> + <constructor-arg value="org.springframework.samples.petclinic.service.ClinicService"/> + </bean> + +</beans> -- GitLab