Testing Spring Data JPA repositories

Testing is an important part of software engineering and with Spring Boot 2.0 @DataJpaTest is introduced to ease the testing of JPA repositories. This annotation will use an embedded database for testing and will auto-configure TestEntityManager to verify the JPA Repository operations.

The following is the test for CommentRepository:

@RunWith(SpringRunner.class)
@DataJpaTest
public class CommentRepoTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private CommentRepository commentRepository;

@Test
public void
findByCreatedYearAndMonthAndDay_HappyPath_ShouldReturn1Comment() {
// Given
Comment comment = new Comment();
comment.setComment("Test");
comment.setType(CommentType.PLUS);
comment.setCreatedDate(new Timestamp(System.currentTimeMillis()));
testEntityManager.persist(comment);
testEntityManager.flush();

// When
LocalDate now = LocalDate.now();
List<Comment> comments =
commentRepository.findByCreatedYearAndMonthAndDay(now.getYear(),
now.getMonth().getValue(), now.getDayOfMonth());

// Then
assertThat(comments).hasSize(1);
assertThat(comments.get(0)).hasFieldOrPropertyWithValue("comment",
"Test");
}

@Test
public void save_HappyPath_ShouldSave1Comment() {
// Given
Comment comment = new Comment();
comment.setComment("Test");
comment.setType(CommentType.PLUS);
comment.setCreatedDate(new Timestamp(System.currentTimeMillis()));

// When
Comment saved = commentRepository.save(comment);

// Then
assertThat(testEntityManager.find(Comment.class,
saved.getId())).isEqualTo(saved);
}
}

The preceding test case uses a TestEntityManager private member auto-wired (auto-wiring private members should be kept to the minimum as it is not a best practice) to the JUnit test and persists Comment by flushing it to the temporary persistence using the TestEntityManager.flush method and then, in one test, tests whether it can be successfully retrieved using the CommentRepository.findByCreatedYearAndMonthAndDay method. Furthermore, in the next test, it tests whether it could successfully save a Comment.

The following is the test for UserRepository:

@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepoTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private UserRepository userRepository;

@Test
public void findByUsername_HappyPath_ShouldReturn1User() throws
Exception {
// Given
User user = new User();
user.setUsername("shazin");
user.setPassword("shaz980");
user.setRole("USER");
testEntityManager.persist(user);
testEntityManager.flush();

// When
User actual = userRepository.findByUsername("shazin");

// Then
assertThat(actual).isEqualTo(user);
}

@Test
public void save_HappyPath_ShouldSave1User() throws Exception {
// Given
User user = new User();
user.setUsername("shazin");
user.setPassword("shaz980");
user.setRole("USER");

// When
User actual = userRepository.save(user);

// Then
assertThat(actual).isNotNull();
assertThat(actual.getId()).isNotNull();
}
}

One test, findByUsername_HappyPath_ShouldReturn1User, in the preceding test case tests the UserRepository.findByUsername method by verifying whether it returns the expected User object with a matching username. The other test, save_HappyPath_ShouldSave1User, tests for the correct persistence of a User object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.38.176