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 the 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 ...