RepositoryBookmarkService implements BookmarkService { @Override public void removeBookmarkOfUser(User user, Project project) { if(userHasBookmark(user, project)) { user.removeBookmark(project); userRepo.save(user); } else { throw new BookmarkNotFoundException(); } } @Autowired  RepositoryBookmarkService(UserRepository userRepo); @Override Project addBookmarkToUser(User user, Project project); @Override void removeBookmarkOfUser(User user, Project project); @Override @Transactional(readOnly = true) List<Project> getBookmarksOfUser(User user); @Override @Transactional(readOnly = true) boolean userHasBookmark(User user, Project project); @Override void removeAllBookmarksOfUser(User user);  }
@Test public void removeBookmarkOfUserRemovesBookmark() { given(userMock.getId()).willReturn(USER_ID); given(userRepository.existsByIdAndBookmarksContaining(USER_ID, projectMock)).willReturn(true); bookmarkService.removeBookmarkOfUser(userMock, projectMock); verify(userMock).removeBookmark(projectMock); verify(userRepository).save(userMock); }
@Test public void removeBookmarkOfUserThrowsExceptionWhenNotBookmarked() { given(userMock.getId()).willReturn(USER_ID); given(userRepository.existsByIdAndBookmarksContaining(USER_ID, projectMock)).willReturn(false); assertThatThrownBy(() -> bookmarkService.removeBookmarkOfUser(userMock, projectMock)) .isInstanceOf(BookmarkNotFoundException.class); }