diff --git a/.mvn/wrapper/maven-wrapper.jar b/.mvn/wrapper/maven-wrapper.jar new file mode 100755 index 00000000..01e67997 Binary files /dev/null and b/.mvn/wrapper/maven-wrapper.jar differ diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100755 index 00000000..b6e67812 --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip \ No newline at end of file diff --git a/pom.xml b/pom.xml index 81e1ff7c..dd448b29 100755 --- a/pom.xml +++ b/pom.xml @@ -341,11 +341,6 @@ org.apache.maven.plugins maven-surefire-plugin 2.22.0 - - - **/*Tests.java - - diff --git a/src/main/java/org/springframework/samples/petclinic/sfg/HearingInterpreter.java b/src/main/java/org/springframework/samples/petclinic/sfg/HearingInterpreter.java new file mode 100644 index 00000000..96267764 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/sfg/HearingInterpreter.java @@ -0,0 +1,24 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.stereotype.Service; + +/** + * Created by jt on 2019-02-16. + */ +@Service +public class HearingInterpreter { + + private final WordProducer wordProducer; + + public HearingInterpreter(WordProducer wordProducer) { + this.wordProducer = wordProducer; + } + + public String whatIheard(){ + String word = wordProducer.getWord(); + + System.out.println(word); + + return word; + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/sfg/LaurelWordProducer.java b/src/main/java/org/springframework/samples/petclinic/sfg/LaurelWordProducer.java new file mode 100644 index 00000000..4921d4ec --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/sfg/LaurelWordProducer.java @@ -0,0 +1,14 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.stereotype.Component; + +/** + * Created by jt on 2019-02-16. + */ +@Component +public class LaurelWordProducer implements WordProducer { + @Override + public String getWord() { + return "Laurel"; + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/sfg/PropertiesWordProducer.java b/src/main/java/org/springframework/samples/petclinic/sfg/PropertiesWordProducer.java new file mode 100644 index 00000000..1ea95572 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/sfg/PropertiesWordProducer.java @@ -0,0 +1,27 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +/** + * Created by jt on 2019-02-18. + */ +@Component +@Profile({"externalized", "laurel-properties"}) +@Primary +public class PropertiesWordProducer implements WordProducer { + + private String word; + + @Value("${say.word}") + public void setWord(String word) { + this.word = word; + } + + @Override + public String getWord() { + return word; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/samples/petclinic/sfg/WordProducer.java b/src/main/java/org/springframework/samples/petclinic/sfg/WordProducer.java new file mode 100644 index 00000000..8b54e388 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/sfg/WordProducer.java @@ -0,0 +1,9 @@ +package org.springframework.samples.petclinic.sfg; + +/** + * Created by jt on 2019-02-16. + */ +public interface WordProducer { + + String getWord(); +} diff --git a/src/main/java/org/springframework/samples/petclinic/sfg/YannyWordProducer.java b/src/main/java/org/springframework/samples/petclinic/sfg/YannyWordProducer.java new file mode 100644 index 00000000..90e6a12f --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/sfg/YannyWordProducer.java @@ -0,0 +1,18 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +/** + * Created by jt on 2019-02-16. + */ +@Profile("yanny") +@Primary +@Component +public class YannyWordProducer implements WordProducer { + @Override + public String getWord() { + return "Yanny"; + } +} diff --git a/src/main/resources/laurel.properties b/src/main/resources/laurel.properties new file mode 100644 index 00000000..a1b3e054 --- /dev/null +++ b/src/main/resources/laurel.properties @@ -0,0 +1 @@ +say.word=LAUrel \ No newline at end of file diff --git a/src/main/resources/yanny.properties b/src/main/resources/yanny.properties new file mode 100644 index 00000000..d4beceee --- /dev/null +++ b/src/main/resources/yanny.properties @@ -0,0 +1 @@ +say.word=YaNNy \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceImplTest.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceImplTest.java new file mode 100644 index 00000000..e6032a08 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceImplTest.java @@ -0,0 +1,52 @@ +package org.springframework.samples.petclinic.service; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.repository.OwnerRepository; +import org.springframework.samples.petclinic.repository.PetRepository; +import org.springframework.samples.petclinic.repository.VetRepository; +import org.springframework.samples.petclinic.repository.VisitRepository; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; + +@ExtendWith(MockitoExtension.class) +class ClinicServiceImplTest { + + @Mock + PetRepository petRepository; + + @Mock + VetRepository vetRepository; + + @Mock + OwnerRepository ownerRepository; + + @Mock + VisitRepository visitRepository; + + @InjectMocks + ClinicServiceImpl service; + + @Test + void findPetTypes() { + //given + List petTypeList = new ArrayList<>(); + given(petRepository.findPetTypes()).willReturn(petTypeList); + //when + Collection returnedPetTypes = service.findPetTypes(); + + //then + then(petRepository).should().findPetTypes(); + assertThat(returnedPetTypes).isNotNull(); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/BaseConfig.java b/src/test/java/org/springframework/samples/petclinic/sfg/BaseConfig.java new file mode 100644 index 00000000..e82c0df1 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/BaseConfig.java @@ -0,0 +1,18 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +/** + * Created by jt on 2019-02-16. + */ +@Profile("base-test") +@Configuration +public class BaseConfig { + + @Bean + HearingInterpreter hearingInterpreter(WordProducer wordProducer){ + return new HearingInterpreter(wordProducer); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/HearingInterpreterTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/HearingInterpreterTest.java new file mode 100644 index 00000000..07d253e3 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/HearingInterpreterTest.java @@ -0,0 +1,27 @@ +package org.springframework.samples.petclinic.sfg; + +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.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@ActiveProfiles("base-test") +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = {BaseConfig.class, LaurelConfig.class}) +public class HearingInterpreterTest { + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + public void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + + assertEquals("Laurel", word); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/HearingInterpreterYannyTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/HearingInterpreterYannyTest.java new file mode 100644 index 00000000..03307dd0 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/HearingInterpreterYannyTest.java @@ -0,0 +1,26 @@ +package org.springframework.samples.petclinic.sfg; + +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.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@ActiveProfiles("base-test") +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = {BaseConfig.class, YannyConfig.class}) +public class HearingInterpreterYannyTest { + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + public void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("Yanny", word); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/LaurelConfig.java b/src/test/java/org/springframework/samples/petclinic/sfg/LaurelConfig.java new file mode 100644 index 00000000..99fede3e --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/LaurelConfig.java @@ -0,0 +1,18 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +/** + * Created by jt on 2019-02-16. + */ +@Profile("base-test") +@Configuration +public class LaurelConfig { + + @Bean + LaurelWordProducer laurelWordProducer(){ + return new LaurelWordProducer(); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/YannyConfig.java b/src/test/java/org/springframework/samples/petclinic/sfg/YannyConfig.java new file mode 100644 index 00000000..392b484b --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/YannyConfig.java @@ -0,0 +1,18 @@ +package org.springframework.samples.petclinic.sfg; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +/** + * Created by jt on 2019-02-16. + */ +@Profile("base-test") +@Configuration +public class YannyConfig { + + @Bean + YannyWordProducer yannyWordProducer(){ + return new YannyWordProducer(); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterActiveProfileTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterActiveProfileTest.java new file mode 100644 index 00000000..7f3c3fff --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterActiveProfileTest.java @@ -0,0 +1,35 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Created by jt on 2019-02-18. + */ +@ActiveProfiles("yanny") +@SpringJUnitConfig(classes = HearingInterpreterActiveProfileTest.TestConfig.class) +public class HearingInterpreterActiveProfileTest { + + @Configuration + @ComponentScan("org.springframework.samples.petclinic.sfg") + static class TestConfig { + + } + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("Yanny", word); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterComponentScanTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterComponentScanTest.java new file mode 100644 index 00000000..c4affa4a --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterComponentScanTest.java @@ -0,0 +1,34 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ActiveProfiles("component-scan") +@SpringJUnitConfig(classes = HearingInterpreterComponentScanTest.TestConfig.class) +class HearingInterpreterComponentScanTest { + + @Profile("component-scan") + @Configuration + @ComponentScan("org.springframework.samples.petclinic.sfg") + static class TestConfig { + + } + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("Laurel", word); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterInnerClassTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterInnerClassTest.java new file mode 100644 index 00000000..a8ee21d6 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterInnerClassTest.java @@ -0,0 +1,38 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.samples.petclinic.sfg.LaurelWordProducer; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ActiveProfiles("inner-class") +@SpringJUnitConfig(classes = HearingInterpreterInnerClassTest.TestConfig.class) +class HearingInterpreterInnerClassTest { + + @Profile("inner-class") + @Configuration + static class TestConfig { + + @Bean + HearingInterpreter hearingInterpreter(){ + return new HearingInterpreter(new LaurelWordProducer()); + } + } + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("Laurel", word); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterLaurelTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterLaurelTest.java new file mode 100644 index 00000000..dce29119 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterLaurelTest.java @@ -0,0 +1,26 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.sfg.BaseConfig; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.samples.petclinic.sfg.LaurelConfig; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ActiveProfiles("base-test") +@SpringJUnitConfig(classes = {BaseConfig.class, LaurelConfig.class}) +class HearingInterpreterLaurelTest { + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("Laurel", word); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterYannyTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterYannyTest.java new file mode 100644 index 00000000..4486d485 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/HearingInterpreterYannyTest.java @@ -0,0 +1,26 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.sfg.BaseConfig; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.samples.petclinic.sfg.YannyConfig; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ActiveProfiles("base-test") +@SpringJUnitConfig(classes = {BaseConfig.class, YannyConfig.class}) +class HearingInterpreterYannyTest { + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("Yanny", word); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/PropertiesLaurelTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/PropertiesLaurelTest.java new file mode 100644 index 00000000..29dbbe8c --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/PropertiesLaurelTest.java @@ -0,0 +1,37 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Created by jt on 2019-02-18. + */ +@TestPropertySource("classpath:laurel.properties") +@ActiveProfiles("laurel-properties") +@SpringJUnitConfig(classes = PropertiesLaurelTest.TestConfig.class) +public class PropertiesLaurelTest { + + @Configuration + @ComponentScan("org.springframework.samples.petclinic.sfg") + static class TestConfig { + + } + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("LAUrel", word); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/sfg/junit5/PropertiesTest.java b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/PropertiesTest.java new file mode 100644 index 00000000..6eb7bfd4 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/sfg/junit5/PropertiesTest.java @@ -0,0 +1,37 @@ +package org.springframework.samples.petclinic.sfg.junit5; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.samples.petclinic.sfg.HearingInterpreter; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Created by jt on 2019-02-18. + */ +@TestPropertySource("classpath:yanny.properties") +@ActiveProfiles("externalized") +@SpringJUnitConfig(classes = PropertiesTest.TestConfig.class) +public class PropertiesTest { + + @Configuration + @ComponentScan("org.springframework.samples.petclinic.sfg") + static class TestConfig { + + } + + @Autowired + HearingInterpreter hearingInterpreter; + + @Test + void whatIheard() { + String word = hearingInterpreter.whatIheard(); + + assertEquals("YaNNy", word); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTest.java b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTest.java new file mode 100644 index 00000000..735f8a6f --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTest.java @@ -0,0 +1,129 @@ +package org.springframework.samples.petclinic.web; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.mockito.Mockito.when; +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.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@SpringJUnitWebConfig(locations = {"classpath:spring/mvc-test-config.xml", "classpath:spring/mvc-core-config.xml"}) +class OwnerControllerTest { + + private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; + + @Autowired + OwnerController ownerController; + + @Autowired + ClinicService clinicService; + + MockMvc mockMvc; + + Owner ownerMock, ownerMock1; + + @BeforeEach + void setUp() { + mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build(); + + // Given + ownerMock = new Owner(); + ownerMock.setId(4); + ownerMock.setFirstName("OwnerMock"); + ownerMock.setLastName(""); + + // Given + ownerMock1 = new Owner(); + ownerMock1.setId(5); + ownerMock1.setFirstName("OwnerMock1"); + ownerMock1.setLastName("Mock1"); + + } + + @Test + // This test has just been rewritten!!!! + void testProcessCreationForm_ValidPath() throws Exception { + // Given + MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); + // The ID is sent as Param, but will not be taken in account. + // Also see the comment in line77 + multiValueMap.add("id","4"); + multiValueMap.add("firstName","Owner"); + multiValueMap.add("lastName","Mock"); + multiValueMap.add("address","Rue des manguiers"); + multiValueMap.add("city","Douala"); + multiValueMap.add("telephone","1234567"); + + mockMvc.perform(post("/owners/new") + .params(multiValueMap)) + .andExpect(status().is3xxRedirection()) + // Doesn't exists in this case +// .andExpect(model().attributeHasNoErrors("firstName,lastName,address,city,telephone")) + // we can not submit an id in the form the way the Controller is implemented!!!! + .andExpect(view().name("redirect:/owners/null")) + .andDo(print()); + } + + @Test + // the new Test + void testProcessCreationForm_InvalidPath() throws Exception { + mockMvc.perform(post("/owners/new")) + .andExpect(status().isOk()) + .andExpect(model().attributeHasFieldErrorCode("owner","lastName","NotEmpty")) + .andExpect(view().name(VIEWS_OWNER_CREATE_OR_UPDATE_FORM)) + .andDo(print()); + } + + @Test + void testFindByName_LastNameNull_OneOwner_Found() throws Exception { + // IntelliJ warns the use of asList() with one element!!!! + // List ownersMock = new ArrayList<>(Arrays.asList(ownerMock)); + + // Given + List ownersMock = Collections.singletonList(ownerMock); + + when(clinicService.findOwnerByLastName(ownerMock.getLastName())).thenReturn(ownersMock); + + // When, Then + mockMvc.perform(get("/owners")) + .andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/owners/"+ownerMock.getId())); + } + + @Test + void testFindByName_LastNameNull_MultOwners_Found() throws Exception { + // Given + List ownersMock = new ArrayList<>(Arrays.asList(ownerMock,ownerMock1)); + + when(clinicService.findOwnerByLastName(ownerMock.getLastName())).thenReturn(ownersMock); + + // When, Then + mockMvc.perform(get("/owners")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("selections")) + .andExpect(view().name("owners/ownersList")); + } + + @Test + void initCreationFormTest() throws Exception { + mockMvc.perform(get("/owners/new")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("owner")) + .andExpect(view().name("owners/createOrUpdateOwnerForm")); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTest.java b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTest.java new file mode 100644 index 00000000..04f21cf3 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/VetControllerTest.java @@ -0,0 +1,80 @@ +package org.springframework.samples.petclinic.web; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.model.Vets; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@ExtendWith(MockitoExtension.class) +class VetControllerTest { + + @Mock + ClinicService clinicService; + + @Mock + Map model; + + @InjectMocks + VetController controller; + + List vetsList = new ArrayList<>(); + + MockMvc mockMvc; + + @BeforeEach + void setUp() { + vetsList.add(new Vet()); + + given(clinicService.findVets()).willReturn(vetsList); + + mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); + } + + @Test + void testControllerShowVetList() throws Exception { + mockMvc.perform(get("/vets.html")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("vets")) + .andExpect(view().name("vets/vetList")); + } + + @Test + void showVetList() { + //when + String view = controller.showVetList(model); + + //then + then(clinicService).should().findVets(); + then(model).should().put(anyString(), any()); + assertThat("vets/VetList").isEqualToIgnoringCase(view); + } + + @Test + void showResourcesVetList() { + //when + Vets vets = controller.showResourcesVetList(); + + //then + then(clinicService).should().findVets(); + assertThat(vets.getVetList()).hasSize(1); + } +} \ No newline at end of file diff --git a/src/test/resources/spring/mvc-test-config.xml b/src/test/resources/spring/mvc-test-config.xml new file mode 100755 index 00000000..ab3b93bc --- /dev/null +++ b/src/test/resources/spring/mvc-test-config.xml @@ -0,0 +1,12 @@ + + + + + + + + +