mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 14:55:50 +00:00
Add unit test for VetController to cover pagination scenario
This commit is contained in:
parent
7d966a0d68
commit
08a9c6e24e
1 changed files with 55 additions and 0 deletions
|
@ -0,0 +1,55 @@
|
||||||
|
package org.springframework.samples.petclinic.vet;
|
||||||
|
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue