mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 23:35:50 +00:00
start Integration tests for PetController
This commit is contained in:
parent
45d16b636b
commit
6b116509b1
15 changed files with 366 additions and 38 deletions
58
pom.xml
58
pom.xml
|
@ -6,13 +6,13 @@
|
||||||
<groupId>org.springframework.samples</groupId>
|
<groupId>org.springframework.samples</groupId>
|
||||||
<artifactId>spring-petclinic</artifactId>
|
<artifactId>spring-petclinic</artifactId>
|
||||||
<version>2.3.0.BUILD-SNAPSHOT</version>
|
<version>2.3.0.BUILD-SNAPSHOT</version>
|
||||||
|
<name>petclinic</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.3.3.RELEASE</version>
|
<version>2.3.3.RELEASE</version>
|
||||||
</parent>
|
</parent>
|
||||||
<name>petclinic</name>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- Generic properties -->
|
<!-- Generic properties -->
|
||||||
|
@ -144,11 +144,6 @@
|
||||||
<version>4.13</version>
|
<version>4.13</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
@ -301,6 +296,56 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<!-- ===================================================== SUREFIRE -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
|
||||||
|
<execution>
|
||||||
|
<id>unit-tests</id>
|
||||||
|
<phase>test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>false</skipTests>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<user.timezone>CET</user.timezone>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test</include>
|
||||||
|
</includes>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>integration-tests</id>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>false</skipTests>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<user.timezone>CET</user.timezone>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest</include>
|
||||||
|
</includes>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*Test</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
@ -405,4 +450,5 @@
|
||||||
</profile>
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -47,13 +47,11 @@ class PetController {
|
||||||
|
|
||||||
private final PetService petService;
|
private final PetService petService;
|
||||||
|
|
||||||
private final PetTypeService petTypeService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
PetController(OwnerService ownerService, PetService petService, PetTypeService petTypeService) {
|
PetController(OwnerService ownerService, PetService petService, PetTypeService petTypeService) {
|
||||||
this.ownerService = ownerService;
|
this.ownerService = ownerService;
|
||||||
this.petService = petService;
|
this.petService = petService;
|
||||||
this.petTypeService = petTypeService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute("types")
|
@ModelAttribute("types")
|
||||||
|
@ -87,10 +85,16 @@ class PetController {
|
||||||
@PostMapping(CommonEndPoint.PETS_NEW)
|
@PostMapping(CommonEndPoint.PETS_NEW)
|
||||||
public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner,
|
public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner,
|
||||||
@ModelAttribute(CommonAttribute.PET) @Valid PetDTO pet, BindingResult result, ModelMap model) {
|
@ModelAttribute(CommonAttribute.PET) @Valid PetDTO pet, BindingResult result, ModelMap model) {
|
||||||
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
if (owner == null) {
|
||||||
result.rejectValue(CommonAttribute.NAME, CommonError.DUPLICATE_ARGS, CommonError.DUPLICATE_MESSAGE);
|
result.rejectValue(CommonAttribute.OWNER, CommonError.NOT_FOUND_ARGS, CommonError.NOT_FOUND_MESSAGE);
|
||||||
|
} else {
|
||||||
|
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
||||||
|
result.rejectValue(CommonAttribute.NAME, CommonError.DUPLICATE_ARGS, CommonError.DUPLICATE_MESSAGE);
|
||||||
|
}
|
||||||
|
owner.addPet(pet);
|
||||||
}
|
}
|
||||||
owner.addPet(pet);
|
|
||||||
|
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.put(CommonAttribute.PET, pet);
|
model.put(CommonAttribute.PET, pet);
|
||||||
return CommonView.PET_CREATE_OR_UPDATE;
|
return CommonView.PET_CREATE_OR_UPDATE;
|
||||||
|
|
|
@ -16,19 +16,24 @@
|
||||||
|
|
||||||
package org.springframework.samples.petclinic;
|
package org.springframework.samples.petclinic;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
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.context.SpringBootTest;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class PetclinicIntegrationTests {
|
class PetclinicIT {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private VetRepository vets;
|
private VetRepository vets;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindAll() throws Exception {
|
void testFindAll() throws Exception {
|
||||||
|
log.info("===================================================================================================");
|
||||||
|
log.info("=== in PetclinicIT ===");
|
||||||
|
log.info("===================================================================================================");
|
||||||
vets.findAll();
|
vets.findAll();
|
||||||
vets.findAll(); // served from cache
|
vets.findAll(); // served from cache
|
||||||
}
|
}
|
|
@ -0,0 +1,125 @@
|
||||||
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
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.OwnerDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||||
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
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.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class OwnerControllerIntegrationTest {
|
||||||
|
|
||||||
|
private static final int TEST_OWNER_ID = 1;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OwnerRepository ownerRepository;
|
||||||
|
|
||||||
|
static private OwnerDTO george;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
george = new OwnerDTO();
|
||||||
|
george.setFirstName("George");
|
||||||
|
george.setLastName("Franklin");
|
||||||
|
george.setAddress("110 W. Liberty St.");
|
||||||
|
george.setCity("Madison");
|
||||||
|
george.setTelephone("6085551023");
|
||||||
|
PetDTO max = new PetDTO();
|
||||||
|
PetTypeDTO dog = new PetTypeDTO();
|
||||||
|
dog.setName("dog");
|
||||||
|
max.setType(dog);
|
||||||
|
max.setName("Max");
|
||||||
|
max.setBirthDate(LocalDate.now());
|
||||||
|
// george.setPetsInternal(Collections.singleton(max));
|
||||||
|
VisitDTO visit = new VisitDTO();
|
||||||
|
visit.setDate(LocalDate.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("initCreationForm")
|
||||||
|
@DisplayName("Verify that the view for new Owner is initialised with new OwnerDTO")
|
||||||
|
void testInitCreationForm() throws Exception {
|
||||||
|
final MvcResult result = mockMvc.perform(get(CommonEndPoint.OWNERS_NEW)).andExpect(status().isOk())
|
||||||
|
.andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE)).andReturn();
|
||||||
|
|
||||||
|
OwnerDTO found = (OwnerDTO) Objects.requireNonNull(result.getModelAndView()).getModel()
|
||||||
|
.get(CommonAttribute.OWNER);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(new OwnerDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
|
void testProcessCreationFormSuccess() throws Exception {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
String json = mapper.writeValueAsString(george);
|
||||||
|
|
||||||
|
final MvcResult result = mockMvc
|
||||||
|
.perform(post(CommonEndPoint.OWNERS_NEW).flashAttr(CommonAttribute.OWNER, george))
|
||||||
|
.andExpect(status().is3xxRedirection()).andReturn();
|
||||||
|
|
||||||
|
json = result.getResponse().getContentAsString(StandardCharsets.UTF_8);
|
||||||
|
OwnerDTO found = mapper.readValue(json, OwnerDTO.class);
|
||||||
|
|
||||||
|
assertThat(found).isEqualTo(george);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
@Tag("processFindForm")
|
||||||
|
@DisplayName("Verify that we get the right view and all Owners")
|
||||||
|
void testProcessFindFormSuccess() throws Exception {
|
||||||
|
List<Owner> expected = ownerRepository.findAll();
|
||||||
|
|
||||||
|
final MvcResult result = mockMvc.perform(get(CommonEndPoint.OWNERS))
|
||||||
|
.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(view().name(CommonView.OWNER_OWNERS_LIST))
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
|
Collection<OwnerDTO> founds = (Collection<OwnerDTO>) result.getModelAndView().getModel()
|
||||||
|
.get(CommonAttribute.SELECTIONS);
|
||||||
|
|
||||||
|
int[] position = new int[] { 0 };
|
||||||
|
|
||||||
|
founds.forEach(ownerDTO -> {
|
||||||
|
Owner owner = expected.get(position[0]++);
|
||||||
|
|
||||||
|
assertThat(owner.getId()).isEqualTo(ownerDTO.getId());
|
||||||
|
assertThat(owner.getFirstName()).isEqualTo(ownerDTO.getFirstName());
|
||||||
|
assertThat(owner.getLastName()).isEqualTo(ownerDTO.getLastName());
|
||||||
|
assertThat(owner.getTelephone()).isEqualTo(ownerDTO.getTelephone());
|
||||||
|
assertThat(owner.getAddress()).isEqualTo(ownerDTO.getAddress());
|
||||||
|
assertThat(owner.getCity()).isEqualTo(ownerDTO.getCity());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ import org.assertj.core.util.Lists;
|
||||||
import org.hamcrest.BaseMatcher;
|
import org.hamcrest.BaseMatcher;
|
||||||
import org.hamcrest.Description;
|
import org.hamcrest.Description;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Tag;
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -58,7 +59,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
@WebMvcTest(OwnerController.class)
|
@WebMvcTest(OwnerController.class)
|
||||||
class OwnerControllerTests {
|
class OwnerControllerTest {
|
||||||
|
|
||||||
private static final int TEST_OWNER_ID = 1;
|
private static final int TEST_OWNER_ID = 1;
|
||||||
|
|
||||||
|
@ -98,6 +99,7 @@ class OwnerControllerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("initCreationForm")
|
@Tag("initCreationForm")
|
||||||
|
@DisplayName("Verify that we get the right view and the right attribute name")
|
||||||
void testInitCreationForm() throws Exception {
|
void testInitCreationForm() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.OWNERS_NEW)).andExpect(status().isOk())
|
mockMvc.perform(get(CommonEndPoint.OWNERS_NEW)).andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists(CommonAttribute.OWNER))
|
.andExpect(model().attributeExists(CommonAttribute.OWNER))
|
||||||
|
@ -107,7 +109,7 @@ class OwnerControllerTests {
|
||||||
@Test
|
@Test
|
||||||
@Tag("processCreationForm")
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormSuccess() throws Exception {
|
void testProcessCreationFormSuccess() throws Exception {
|
||||||
mockMvc.perform(post("/owners/new").param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
|
mockMvc.perform(post(CommonEndPoint.OWNERS_NEW).param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
|
||||||
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
|
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
|
||||||
.param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street").param(CommonAttribute.OWNER_CITY, "London")
|
.param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street").param(CommonAttribute.OWNER_CITY, "London")
|
||||||
.param(CommonAttribute.OWNER_PHONE, "01316761638")).andExpect(status().is3xxRedirection());
|
.param(CommonAttribute.OWNER_PHONE, "01316761638")).andExpect(status().is3xxRedirection());
|
||||||
|
@ -134,6 +136,7 @@ class OwnerControllerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("processFindForm")
|
@Tag("processFindForm")
|
||||||
|
@DisplayName("Verify that we get the right view and all Owners list")
|
||||||
void testProcessFindFormSuccess() throws Exception {
|
void testProcessFindFormSuccess() throws Exception {
|
||||||
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO()));
|
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO()));
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
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.OwnerDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
|
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.service.OwnerService;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
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.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
class PetControllerIntegrationTest {
|
||||||
|
|
||||||
|
private static final int TEST_OWNER_ID = 5;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OwnerService ownerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PetRepository petRepository;
|
||||||
|
|
||||||
|
private OwnerDTO ownerDTO;
|
||||||
|
|
||||||
|
private PetDTO petDTO;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void beforeEach() {
|
||||||
|
ownerDTO = ownerService.findById(TEST_OWNER_ID);
|
||||||
|
petDTO = new PetDTO();
|
||||||
|
PetType type = petRepository.findPetTypes().get(1);
|
||||||
|
PetTypeDTO typeDTO = new PetTypeDTO();
|
||||||
|
typeDTO.setId(type.getId());
|
||||||
|
typeDTO.setName(type.getName());
|
||||||
|
petDTO.setType(typeDTO);
|
||||||
|
petDTO.setName("Max");
|
||||||
|
petDTO.setBirthDate(LocalDate.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("initCreationForm")
|
||||||
|
@DisplayName("Verify that the view for new Pet is initialised with new PetDTO")
|
||||||
|
void testInitCreationForm() throws Exception {
|
||||||
|
|
||||||
|
final MvcResult result = mockMvc
|
||||||
|
.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
|
.flashAttr(CommonAttribute.OWNER, ownerDTO))
|
||||||
|
.andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE)).andReturn();
|
||||||
|
|
||||||
|
PetDTO petFound = (PetDTO) Objects.requireNonNull(result.getModelAndView()).getModel().get(CommonAttribute.PET);
|
||||||
|
|
||||||
|
assertThat(petFound.getOwner()).isEqualTo(ownerDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
|
@DisplayName("Verify that save new Pet and display the view")
|
||||||
|
void testProcessCreationFormSuccess() throws Exception {
|
||||||
|
|
||||||
|
final MvcResult result = mockMvc
|
||||||
|
.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
|
.flashAttr(CommonAttribute.OWNER, ownerDTO).flashAttr(CommonAttribute.PET, petDTO))
|
||||||
|
.andExpect(status().is3xxRedirection()).andExpect(view().name(CommonView.OWNER_OWNERS_ID_R))
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
|
List<PetDTO> found = ownerService.findById(TEST_OWNER_ID).getPets();
|
||||||
|
|
||||||
|
assertThat(found).contains(petDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -25,6 +25,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
|
|
||||||
import org.assertj.core.util.Lists;
|
import org.assertj.core.util.Lists;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Tag;
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -53,10 +54,9 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||||
*/
|
*/
|
||||||
@WebMvcTest(value = PetController.class,
|
@WebMvcTest(value = PetController.class,
|
||||||
includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
|
includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
|
||||||
class PetControllerTests {
|
class PetControllerTest {
|
||||||
|
|
||||||
private static final int TEST_OWNER_ID = 1;
|
private static final int TEST_OWNER_ID = 1;
|
||||||
|
|
||||||
private static final int TEST_PET_ID = 1;
|
private static final int TEST_PET_ID = 1;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -72,7 +72,7 @@ class PetControllerTests {
|
||||||
private OwnerService ownerService;
|
private OwnerService ownerService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void beforeEach() {
|
||||||
PetTypeDTO cat = new PetTypeDTO();
|
PetTypeDTO cat = new PetTypeDTO();
|
||||||
cat.setId(3);
|
cat.setId(3);
|
||||||
cat.setName("hamster");
|
cat.setName("hamster");
|
||||||
|
@ -84,6 +84,7 @@ class PetControllerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("initCreationForm")
|
@Tag("initCreationForm")
|
||||||
|
@DisplayName("Verify that Pet creation form is initialized")
|
||||||
void testInitCreationForm() throws Exception {
|
void testInitCreationForm() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID))
|
mockMvc.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID))
|
||||||
.andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE))
|
.andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE))
|
||||||
|
@ -92,24 +93,74 @@ class PetControllerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("processCreationForm")
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormSuccess() throws Exception {
|
@DisplayName("Verify that call the right view with parameters when attempt to create Pet")
|
||||||
|
void givenPet_WhenPostPet_thenRedirectToOwnerForm() throws Exception {
|
||||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
.param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_TYPE, "hamster")
|
.param(CommonAttribute.PET_NAME, "Betty")
|
||||||
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12")).andExpect(status().is3xxRedirection())
|
.param(CommonAttribute.PET_TYPE, "hamster")
|
||||||
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||||
|
.andExpect(status().is3xxRedirection())
|
||||||
|
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("processCreationForm")
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormHasErrors() throws Exception {
|
@DisplayName("Verify that return to Pet creation form when pet has no name")
|
||||||
|
void givenPetWithNoName_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
.param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
.param(CommonAttribute.PET_TYPE, "hamster")
|
||||||
.andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
|
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||||
.andExpect(model().attributeHasErrors(CommonAttribute.PET))
|
.andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
|
||||||
.andExpect(model().attributeHasFieldErrors(CommonAttribute.PET, CommonAttribute.PET_TYPE))
|
.andExpect(model().attributeHasErrors(CommonAttribute.PET))
|
||||||
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.PET_TYPE,
|
.andExpect(model().attributeHasFieldErrors(CommonAttribute.PET, CommonAttribute.PET_NAME))
|
||||||
CommonError.REQUIRED_ARGS))
|
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.PET_NAME, CommonError.REQUIRED_ARGS))
|
||||||
.andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
|
@DisplayName("Verify that return to Pet creation form when pet has no type")
|
||||||
|
void givenPetWithNoType_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||||
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
|
.param(CommonAttribute.PET_NAME, "Betty")
|
||||||
|
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||||
|
.andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
|
||||||
|
.andExpect(model().attributeHasErrors(CommonAttribute.PET))
|
||||||
|
.andExpect(model().attributeHasFieldErrors(CommonAttribute.PET, CommonAttribute.PET_TYPE))
|
||||||
|
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.PET_TYPE, CommonError.REQUIRED_ARGS))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
|
@DisplayName("Verify that return to Pet creation form when pet has wrong Owner ID")
|
||||||
|
void givenPetWithWrongOwnerId_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||||
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, 22)
|
||||||
|
.param(CommonAttribute.PET_NAME, "Betty")
|
||||||
|
.param(CommonAttribute.PET_TYPE, "hamster")
|
||||||
|
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||||
|
.andExpect(model().attributeHasErrors(CommonAttribute.PET))
|
||||||
|
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.OWNER, CommonError.NOT_FOUND_ARGS))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
|
@DisplayName("Verify that return to Pet creation form when pet has no birth date")
|
||||||
|
void givenPetWithNoBirthDate_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||||
|
|
||||||
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
|
.param(CommonAttribute.PET_NAME, "Betty")
|
||||||
|
.param(CommonAttribute.PET_TYPE, "hamster"))
|
||||||
|
.andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
|
||||||
|
.andExpect(model().attributeHasErrors(CommonAttribute.PET))
|
||||||
|
.andExpect(model().attributeHasFieldErrors(CommonAttribute.PET, CommonAttribute.PET_BIRTH_DATE))
|
||||||
|
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.PET_BIRTH_DATE, CommonError.REQUIRED_ARGS))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
|
@ -48,7 +48,7 @@ import org.springframework.test.web.servlet.ResultActions;
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
@WebMvcTest(VetController.class)
|
@WebMvcTest(VetController.class)
|
||||||
class VetControllerTests {
|
class VetControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
|
@ -43,7 +43,7 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||||
* @author Colin But
|
* @author Colin But
|
||||||
*/
|
*/
|
||||||
@WebMvcTest(VisitController.class)
|
@WebMvcTest(VisitController.class)
|
||||||
class VisitControllerTests {
|
class VisitControllerTest {
|
||||||
|
|
||||||
private static final int TEST_PET_ID = 1;
|
private static final int TEST_PET_ID = 1;
|
||||||
|
|
|
@ -43,7 +43,7 @@ import static org.mockito.BDDMockito.given;
|
||||||
* @author Colin But
|
* @author Colin But
|
||||||
*/
|
*/
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class PetTypeDTOFormatterTests {
|
class PetTypeDTOFormatterTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private PetService petService;
|
private PetService petService;
|
|
@ -43,7 +43,7 @@ import static org.mockito.BDDMockito.given;
|
||||||
* @author Colin But
|
* @author Colin But
|
||||||
*/
|
*/
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class PetTypeFormatterTests {
|
class PetTypeFormatterTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private PetService petService;
|
private PetService petService;
|
|
@ -23,7 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
/**
|
/**
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
class VetTests {
|
class VetTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSerialization() {
|
void testSerialization() {
|
|
@ -49,7 +49,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
* <li><strong>Dependency Injection</strong> of test fixture instances, meaning that we
|
* <li><strong>Dependency Injection</strong> of test fixture instances, meaning that we
|
||||||
* don't need to perform application context lookups. See the use of
|
* don't need to perform application context lookups. See the use of
|
||||||
* {@link Autowired @Autowired} on the <code>{@link
|
* {@link Autowired @Autowired} on the <code>{@link
|
||||||
* ClinicServiceTests#clinicService clinicService}</code> instance variable, which uses
|
* ClinicServiceTest#clinicService clinicService}</code> instance variable, which uses
|
||||||
* autowiring <em>by type</em>.
|
* autowiring <em>by type</em>.
|
||||||
* <li><strong>Transaction management</strong>, meaning each test method is executed in
|
* <li><strong>Transaction management</strong>, meaning each test method is executed in
|
||||||
* its own transaction, which is automatically rolled back by default. Thus, even if tests
|
* its own transaction, which is automatically rolled back by default. Thus, even if tests
|
||||||
|
@ -67,7 +67,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
|
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
|
||||||
class ClinicServiceTests {
|
class ClinicServiceTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected OwnerRepository owners;
|
protected OwnerRepository owners;
|
|
@ -36,7 +36,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
// Waiting https://github.com/spring-projects/spring-boot/issues/5574
|
// Waiting https://github.com/spring-projects/spring-boot/issues/5574
|
||||||
@Disabled
|
@Disabled
|
||||||
@WebMvcTest(controllers = CrashController.class)
|
@WebMvcTest(controllers = CrashController.class)
|
||||||
class CrashControllerTests {
|
class CrashControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
|
@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @author Michael Isvy Simple test to make sure that Bean Validation is working (useful
|
* @author Michael Isvy Simple test to make sure that Bean Validation is working (useful
|
||||||
* when upgrading to a new version of Hibernate Validator/ Bean Validation)
|
* when upgrading to a new version of Hibernate Validator/ Bean Validation)
|
||||||
*/
|
*/
|
||||||
class ValidatorTests {
|
class ValidatorTest {
|
||||||
|
|
||||||
private Validator createValidator() {
|
private Validator createValidator() {
|
||||||
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
|
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
|
Loading…
Reference in a new issue