-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Spring MockMVC tests for visit controller. Closes #66
- Loading branch information
1 parent
6d21a9e
commit 233a5b0
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
pet-clinic-web/src/test/java/org/nickharle/osgpetclinic/controllers/VisitControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
; | ||
} | ||
} |