FavoriteService { public Favorite addFavorite(Favorite favorite) { UserInfo user = userService.findByUserId(favorite.getUserId()); if (user == null) { throw new BadRequestException("user not exist"); } UserInfo loginUser = userInfoHolder.getUser(); if (!loginUser.equals(user)) { throw new BadRequestException("add favorite fail. " + "because favorite's user is not current login user."); } Favorite checkedFavorite = favoriteRepository.findByUserIdAndAppId(loginUser.getUserId(), favorite.getAppId()); if (checkedFavorite != null) { return checkedFavorite; } favorite.setPosition(POSITION_DEFAULT); favorite.setDataChangeCreatedBy(user.getUserId()); favorite.setDataChangeLastModifiedBy(user.getUserId()); return favoriteRepository.save(favorite); } FavoriteService(
      final UserInfoHolder userInfoHolder,
      final FavoriteRepository favoriteRepository,
      final UserService userService); Favorite addFavorite(Favorite favorite); List<Favorite> search(String userId, String appId, Pageable page); void deleteFavorite(long favoriteId); void adjustFavoriteToFirst(long favoriteId); void batchDeleteByAppId(String appId, String operator); static final long POSITION_DEFAULT; }
@Test(expected = BadRequestException.class) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testAddFavoriteErrorUser() { String testApp = "testApp"; Favorite favorite = instanceOfFavorite("errorUser", testApp); favoriteService.addFavorite(favorite); }