Skip to content

Commit

Permalink
Create Spring MockMVC tests for visit controller. Closes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
OldScotsGuy committed Jul 13, 2019
1 parent 6d21a9e commit 233a5b0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
import java.util.Map;

@Controller
Expand All @@ -26,6 +28,14 @@ public VisitController(VisitService visitService, PetService petService) {
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");

// Take in a string and parse to a LocalData
dataBinder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException{
setValue(LocalDate.parse(text));
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.nickharle.osgpetclinic.controllers;

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.nickharle.osgpetclinic.model.Owner;
import org.nickharle.osgpetclinic.model.Pet;
import org.nickharle.osgpetclinic.model.PetType;
import org.nickharle.osgpetclinic.services.PetService;
import org.nickharle.osgpetclinic.services.VisitService;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.util.UriTemplate;

import java.net.URI;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import static org.mockito.ArgumentMatchers.anyLong;
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.MockMvcResultMatchers.*;

@ExtendWith(MockitoExtension.class)
class VisitControllerTest {

private static final String PETS_CREATE_OR_UPDATE_VISIT_FORM = "pets/createOrUpdateVisitForm";
private static final String REDIRECT_OWNERS_1 = "redirect:/owners/{ownerId}";
private static final String YET_ANOTHER_VISIT_DESCRIPTION = "yet another visit";

@Mock
PetService petService;

@Mock
VisitService visitService;

@InjectMocks
VisitController visitController;

private MockMvc mockMvc;
private final UriTemplate visitsUriTemplate = new UriTemplate("/owners/{ownerId}/pets/{petId}/visits/new");
private final Map<String, String> uriVariables = new HashMap<>();
private URI visitsUri;

@BeforeEach
void setUp() {
Long petId = 1L;
Long ownerId = 1L;
when(petService.findById(anyLong()))
.thenReturn(
Pet.builder()
.id(petId)
.birthDate(LocalDate.of(2018,11,13))
.name("Cutie")
.visits(new HashSet<>())
.owner(Owner.builder()
.id(ownerId)
.lastName("Doe")
.firstName("Joe")
.build())
.petType(PetType.builder()
.name("Dog").build())
.build()
);

uriVariables.clear();
uriVariables.put("ownerId", ownerId.toString());
uriVariables.put("petId", petId.toString());
visitsUri = visitsUriTemplate.expand(uriVariables);

mockMvc = MockMvcBuilders
.standaloneSetup(visitController)
.build();
}

@Test
void initNewVisitForm() throws Exception {
mockMvc.perform(get(visitsUri))
.andExpect(status().isOk())
.andExpect(view().name(PETS_CREATE_OR_UPDATE_VISIT_FORM))
;
}

@Test
void processNewVisitForm() throws Exception {
mockMvc.perform(post(visitsUri)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("date","2018-11-11")
.param("description", YET_ANOTHER_VISIT_DESCRIPTION))
.andExpect(status().is3xxRedirection())
.andExpect(view().name(REDIRECT_OWNERS_1))
.andExpect(model().attributeExists("visit"))
;
}
}

0 comments on commit 233a5b0

Please sign in to comment.