Let's write a few tests to use the sorting and pagination capabilities of UserRepository. The base of the test is very similar to TodoRepositoryTest:
@DataJpaTest
@RunWith(SpringRunner.class)
public class UserRepositoryTest {
@Autowired
UserRepository userRepository;
@Autowired
TestEntityManager entityManager;
}
Let's write a simple test to sort users and print users to the log:
@Test
public void testing_sort_stuff() {
Sort sort = new Sort(Sort.Direction.DESC, "name")
.and(new Sort(Sort.Direction.ASC, "userid"));
Iterable<User> users = userRepository.findAll(sort);
for (User user : users) {
System.out.println(user);
}
}
Important things to note are as follows:
- new Sort(Sort.Direction.DESC, "name"): We would want to sort by name in descending order.
- and(new Sort(Sort.Direction.ASC, "userid")): The and() method is a conjunction method used to combine different sort configurations. In this example, we are adding a secondary criteria to sort by the user ID in the ascending order.
- userRepository.findAll(sort): The sort criteria are passed as a parameter to the findAll() method.
The output of the preceding test is shown as follows. The users are sorted in descending order by name:
User [id=4, userid=UserId4, name=User Name 4, todos=0]
User [id=3, userid=UserId3, name=User Name 3, todos=0]
User [id=2, userid=UserId2, name=User Name 2, todos=1]
User [id=1, userid=UserId1, name=User Name 1, todos=2]
The test for pageable is as follows:
@Test
public void using_pageable_stuff() {
PageRequest pageable = new PageRequest(0, 2);
Page<User> userPage = userRepository.findAll(pageable);
System.out.println(userPage);
System.out.println(userPage.getContent());
}
The output of the test is as follows:
Page 1 of 2 containing com.in28minutes.model.User instances
[User [id=1, userid=UserId1, name=User Name 1, todos=2],
User [id=2, userid=UserId2, name=User Name 2, todos=1]]
Important things to note are as follows:
- new PageRequest(0, 2): We are requesting the first page (index 0) and setting the size of each page to 2.
- userRepository.findAll(pageable): The PageRequest object is sent as a parameter to the findAll method.
- Page 1 of 2: The output shows that we are looking at the first page in a total of 2 pages.
A couple of important things to note about PageRequest are as follows:
- The PageRequest object has the next(), previous(), and first() methods to traverse the pages.
- The PageRequest constructor (public PageRequest(int page, int size, Sort sort)) also accepts a third parameter—Sort order.
Important methods in Page and its child interface, Slice, are listed as follows:
- int getTotalPages(): Returns the number of result pages
- long getTotalElements(): Returns the total number of elements in all pages
- int getNumber(): Returns the number of the current page
- int getNumberOfElements(): Returns the number of elements in the current page
- List<T> getContent(): Gets the content of the current slice (or page) as a list
- boolean hasContent(): Returns if the current slice has any elements
- boolean isFirst(): Returns if this is the first slice
- boolean isLast(): Returns if this is the last slice
- boolean hasNext(): Returns if there is a next slice
- boolean hasPrevious(): Returns if there is a previous slice
- Pageable nextPageable(): Gets access to the next slice
- Pageable previousPageable(): Gets access to the previous slice