Create HibernateUserRepositoryTests

In HibernateUserRepositoryTests, we will create the following test methods to cover different scenarios:

  • save_nullUsernameUser_shouldFail()
  • save_nullEmailAddressUser_shouldFail()
  • save_nullPasswordUser_shouldFail()
  • save_validUser_shouldSuccess()
  • save_usernameAlreadyExist_shouldFail()
  • save_emailAddressAlreadyExist_shouldFail()
  • findByEmailAddress_notExist_shouldReturnEmptyResult()
  • findByEmailAddress_exist_shouldReturnResult()
  • findByUsername_notExist_shouldReturnEmptyResult()
  • findByUsername_exist_shouldReturnResult()

First of all, let's add a H2 dependency into Maven's pom.xml as follows, so that when Spring Boot starts the tests, it knows that we need H2 instead of MySQL:

<dependency> <groupId>com.h2database</groupId> ...

Get Building Applications with Spring 5 and Vue.js 2 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.