Unit tests

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

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.