mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 06:45:50 +00:00
Add unit test for VetController.showResourcesVetList
This commit is contained in:
parent
625eb7ef14
commit
69c996cedb
1 changed files with 44 additions and 160 deletions
|
@ -1,186 +1,70 @@
|
|||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.samples.petclinic.vet;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
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.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.ui.Model;
|
||||
import java.util.Collections;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class VetControllerTest {
|
||||
|
||||
@Mock
|
||||
private VetRepository vetRepository;
|
||||
|
||||
@Mock
|
||||
private Model model;
|
||||
|
||||
@InjectMocks
|
||||
private VetController vetController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
void setup() {
|
||||
vetRepository = mock(VetRepository.class);
|
||||
vetController = new VetController(vetRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test showVetList with pagination")
|
||||
void testShowVetListWithPagination() {
|
||||
int page = 1;
|
||||
Pageable pageable = PageRequest.of(page - 1, 5);
|
||||
Page<Vet> paginatedVets = new PageImpl<>(Collections.emptyList(), pageable, 0);
|
||||
@DisplayName("Test showResourcesVetList returns correct list of veterinarians")
|
||||
void testShowResourcesVetList() {
|
||||
// Given
|
||||
Vet vet1 = new Vet();
|
||||
vet1.setFirstName("John");
|
||||
vet1.setLastName("Doe");
|
||||
|
||||
doReturn(paginatedVets).when(vetRepository).findAll(pageable);
|
||||
Vet vet2 = new Vet();
|
||||
vet2.setFirstName("Jane");
|
||||
vet2.setLastName("Smith");
|
||||
|
||||
String viewName = vetController.showVetList(page, model);
|
||||
List<Vet> vetsList = Arrays.asList(vet1, vet2);
|
||||
given(vetRepository.findAll()).willReturn(vetsList);
|
||||
|
||||
assertThat(viewName).isEqualTo("vets/vetList");
|
||||
verify(vetRepository).findAll(pageable);
|
||||
verify(model).addAttribute("currentPage", page);
|
||||
verify(model).addAttribute("totalPages", paginatedVets.getTotalPages());
|
||||
verify(model).addAttribute("totalItems", paginatedVets.getTotalElements());
|
||||
verify(model).addAttribute("listVets", paginatedVets.getContent());
|
||||
}
|
||||
// When
|
||||
Vets vets = vetController.showResourcesVetList();
|
||||
|
||||
@Test
|
||||
@DisplayName("Test showVetList without pagination")
|
||||
void testShowVetListWithoutPagination() {
|
||||
int defaultPage = 1;
|
||||
Pageable pageable = PageRequest.of(defaultPage - 1, 5);
|
||||
Page<Vet> paginatedVets = new PageImpl<>(Collections.emptyList(), pageable, 0);
|
||||
|
||||
doReturn(paginatedVets).when(vetRepository).findAll(pageable);
|
||||
|
||||
String viewName = vetController.showVetList(defaultPage, model);
|
||||
|
||||
assertThat(viewName).isEqualTo("vets/vetList");
|
||||
verify(vetRepository).findAll(pageable);
|
||||
verify(model).addAttribute("currentPage", defaultPage);
|
||||
verify(model).addAttribute("totalPages", paginatedVets.getTotalPages());
|
||||
verify(model).addAttribute("totalItems", paginatedVets.getTotalElements());
|
||||
verify(model).addAttribute("listVets", paginatedVets.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test showVetList with non-empty paginated result")
|
||||
void testShowVetListWithNonEmptyPagination() {
|
||||
int page = 1;
|
||||
Pageable pageable = PageRequest.of(page - 1, 5);
|
||||
Vet vet = new Vet();
|
||||
Page<Vet> paginatedVets = new PageImpl<>(Collections.singletonList(vet), pageable, 1);
|
||||
|
||||
doReturn(paginatedVets).when(vetRepository).findAll(pageable);
|
||||
|
||||
String viewName = vetController.showVetList(page, model);
|
||||
|
||||
assertThat(viewName).isEqualTo("vets/vetList");
|
||||
verify(vetRepository).findAll(pageable);
|
||||
verify(model).addAttribute("currentPage", page);
|
||||
verify(model).addAttribute("totalPages", paginatedVets.getTotalPages());
|
||||
verify(model).addAttribute("totalItems", paginatedVets.getTotalElements());
|
||||
verify(model).addAttribute("listVets", paginatedVets.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test showVetList with empty database")
|
||||
void testShowVetListWithEmptyDatabase() {
|
||||
int page = 1;
|
||||
Pageable pageable = PageRequest.of(page - 1, 5);
|
||||
Page<Vet> paginatedVets = new PageImpl<>(Collections.emptyList(), pageable, 0);
|
||||
|
||||
doReturn(paginatedVets).when(vetRepository).findAll(pageable);
|
||||
|
||||
String viewName = vetController.showVetList(page, model);
|
||||
|
||||
assertThat(viewName).isEqualTo("vets/vetList");
|
||||
verify(vetRepository).findAll(pageable);
|
||||
verify(model).addAttribute("currentPage", page);
|
||||
verify(model).addAttribute("totalPages", paginatedVets.getTotalPages());
|
||||
verify(model).addAttribute("totalItems", paginatedVets.getTotalElements());
|
||||
verify(model).addAttribute("listVets", paginatedVets.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test showVetList with page 2 and empty result")
|
||||
void testShowVetListWithPage2AndEmptyResult() {
|
||||
int page = 2;
|
||||
Pageable pageable = PageRequest.of(page - 1, 5);
|
||||
Page<Vet> paginatedVets = new PageImpl<>(Collections.emptyList(), pageable, 0);
|
||||
|
||||
doReturn(paginatedVets).when(vetRepository).findAll(pageable);
|
||||
|
||||
String viewName = vetController.showVetList(page, model);
|
||||
|
||||
assertThat(viewName).isEqualTo("vets/vetList");
|
||||
verify(vetRepository).findAll(pageable);
|
||||
verify(model).addAttribute("currentPage", page);
|
||||
verify(model).addAttribute("totalPages", paginatedVets.getTotalPages());
|
||||
verify(model).addAttribute("totalItems", paginatedVets.getTotalElements());
|
||||
verify(model).addAttribute("listVets", paginatedVets.getContent());
|
||||
// Then
|
||||
assertThat(vets).isNotNull();
|
||||
assertThat(vets.getVetList()).hasSize(2);
|
||||
assertThat(vets.getVetList()).extracting(Vet::getFirstName).containsExactly("John", "Jane");
|
||||
assertThat(vets.getVetList()).extracting(Vet::getLastName).containsExactly("Doe", "Smith");
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.samples.petclinic.vet;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
class VetControllerTest {
|
||||
|
||||
@Mock
|
||||
private VetRepository vetRepository;
|
||||
|
||||
@InjectMocks
|
||||
private VetController vetController;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(vetController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testShowResourcesVetList() throws Exception {
|
||||
Vet vet1 = new Vet();
|
||||
vet1.setId(1);
|
||||
vet1.setFirstName("James");
|
||||
vet1.setLastName("Carter");
|
||||
Vet vet2 = new Vet();
|
||||
vet2.setId(2);
|
||||
vet2.setFirstName("Helen");
|
||||
vet2.setLastName("Leary");
|
||||
|
||||
given(vetRepository.findAll()).willReturn(Arrays.asList(vet1, vet2));
|
||||
|
||||
mockMvc.perform(get("/vets")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().json("{"vetList":[{"id":1,"firstName":"James","lastName":"Carter","specialties":[]},{"id":2,"firstName":"Helen","lastName":"Leary","specialties":[]}]}", true));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue