Testing Services

Services with business logic need to be tested for their correct functionality. In order to do this, Services like the following one can be used:

@RunWith(SpringRunner.class)
public class CommentServiceTest {

@MockBean
private CommentRepository commentRepository;

private CommentService commentService;

@Before
public void init() {
commentService = new CommentService(commentRepository);
}

@Test
public void
getAllCommentsForToday_HappyPath_ShouldReturn1Comment() {
// Given
Comment comment = new Comment();
comment.setComment("Test");
comment.setType(CommentType.PLUS);
comment.setCreatedDate(new
Timestamp(System.currentTimeMillis()));
List<Comment> comments = Arrays.asList(comment);
LocalDate now = LocalDate.now();


when
(commentRepository.findByCreatedYearAndMonthAndDay(now.getYear(), now.getMonth().getValue(),
now.getDayOfMonth())).thenReturn(comments);

// When
List<Comment> actualComments =
commentService.getAllCommentsForToday();

// Then
verify(commentRepository,
times(1)).findByCreatedYearAndMonthAndDay(now.getYear(),
now.getMonth().getValue(), now.getDayOfMonth());
assertThat(comments).isEqualTo(actualComments);
}

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

Comment comment2 = new Comment();
comment2.setComment("Test Star");
comment2.setType(CommentType.STAR);
comment2.setCreatedBy("Shahim");
comment2.setCreatedDate(new
Timestamp(System.currentTimeMillis()));
List<Comment> comments = Arrays.asList(comment, comment2);
when(commentRepository.saveAll(comments)).thenReturn(comments);

// When
List<Comment> saved = commentService.saveAll(comments);

// Then
assertThat(saved).isNotEmpty();
verify(commentRepository, times(1)).saveAll(comments);

}
}

In the preceding test case for CommentServiceCommentRepository is annotated with @MockBean, as testing of its functionality has already been done in the JPA repository testing. During Service, test mocking is done using the Mockito library to just mock repository method invocations and verify the correct invocation.

The following is the service test case for UserService:

@RunWith(SpringRunner.class)
public class UserServiceTest {

@MockBean
private UserRepository userRepository;

private UserService userService;

@Before
public void init() {
this.userService = new UserService(userRepository);
}

@Test
public void getAllCommentsForToday_HappyPath_ShouldReturn1Comment() {
// Given
User user = new User();
user.setUsername("shazin");
user.setPassword("sha908");
user.setRole("USER");

when(userRepository.findByUsername("shazin")).thenReturn(user);

// When
UserDetails actual = userService.loadUserByUsername("shazin");

// Then
verify(userRepository, times(1)).findByUsername("shazin");
}

}

In the preceding test case, the UserRepository.findByUsername method is mocked to return a given user and finally verify whether that method is invoked exactly once.

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

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