add VetControllerIntegrationTest

This commit is contained in:
PEDSF 2020-10-29 18:18:12 +01:00
parent 93cde700ad
commit 6b931913ec
3 changed files with 33 additions and 13 deletions

View file

@ -17,7 +17,6 @@ package org.springframework.samples.petclinic.dto;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.samples.petclinic.model.Specialty;
import javax.xml.bind.annotation.XmlElement;
import java.util.*;

View file

@ -1,17 +1,19 @@
package org.springframework.samples.petclinic.controller;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.modelmapper.internal.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.samples.petclinic.common.CommonAttribute;
import org.springframework.samples.petclinic.common.CommonEndPoint;
import org.springframework.samples.petclinic.common.CommonView;
import org.springframework.samples.petclinic.dto.VetDTO;
import org.springframework.samples.petclinic.dto.VetsDTO;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.repository.VetRepository;
@ -19,10 +21,9 @@ import org.springframework.samples.petclinic.service.VetService;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -45,22 +46,22 @@ public class VetControllerIntegrationTest {
@Autowired
private VetRepository vetRepository;
private VetsDTO expected;
@BeforeEach
void beforeEach() {
Collection<Vet> vets = vetRepository.findAll();
expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets)));
}
@Test
@Tag("showVetList")
void testShowVetListHtml() throws Exception {
Collection<Vet> vets = vetRepository.findAll();
VetsDTO expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets)));
@DisplayName("When asking vets get String containing Vets")
void whenGetVets_thenReturnStringOfVets() throws Exception {
final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS_HTML))
.andExpect(status().isOk())
.andExpect(status().is2xxSuccessful())
.andExpect(model().attributeExists(CommonAttribute.VETS))
.andExpect(view().name(CommonView.VET_VETS_LIST))
.andReturn();
@ -70,5 +71,22 @@ public class VetControllerIntegrationTest {
assertThat(found).isEqualToComparingFieldByField(expected);
}
@Test
@Tag("showResourcesVetList")
@DisplayName("When asking vets get Vets DTO object containing Vets")
void whenGetVets_thenReturnVetsDTO() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS))
.andExpect(status().is2xxSuccessful())
.andReturn();
String json = result.getResponse().getContentAsString(StandardCharsets.UTF_8);
VetsDTO found = mapper.readValue(json, VetsDTO.class);
assertThat(found).isEqualToComparingFieldByField(expected);
}
}

View file

@ -26,6 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@ -75,7 +76,8 @@ class VetControllerTest {
@Test
@Tag("showVetList")
void testShowVetListHtml() throws Exception {
@DisplayName("When asking vets get String containing Vets")
void whenGetVets_thenReturnStringOfVets() throws Exception {
mockMvc.perform(get(CommonEndPoint.VETS_HTML)).andExpect(status().isOk())
.andExpect(model().attributeExists(CommonAttribute.VETS))
.andExpect(view().name(CommonView.VET_VETS_LIST));
@ -83,7 +85,8 @@ class VetControllerTest {
@Test
@Tag("showResourcesVetList")
void testShowResourcesVetList() throws Exception {
@DisplayName("When asking vets get Vets DTO object containing Vets")
void whenGetVets_thenReturnVetsDTO() throws Exception {
ResultActions actions = mockMvc.perform(get(CommonEndPoint.VETS).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());