Skip to content

Commit

Permalink
test: 친구신청 수락 service 테스트코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
minahYu committed Jun 18, 2024
1 parent 36c8fff commit 7095dc6
Showing 1 changed file with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kpring.user.global.CommonTest
import kpring.user.repository.FriendRepository
import kpring.user.repository.UserRepository
import org.springframework.security.crypto.password.PasswordEncoder
import java.util.*

internal class FriendServiceImplTest : FunSpec({
val userRepository: UserRepository = mockk()
Expand Down Expand Up @@ -107,7 +108,10 @@ internal class FriendServiceImplTest : FunSpec({
val friendList = listOf(mockk<Friend>(relaxed = true))

every {
friendRepository.findAllByUserIdAndRequestStatus(CommonTest.TEST_USER_ID, FriendRequestStatus.RECEIVED)
friendRepository.findAllByUserIdAndRequestStatus(
CommonTest.TEST_USER_ID,
FriendRequestStatus.RECEIVED,
)
} returns friendList

val response = friendService.getFriendRequests(CommonTest.TEST_USER_ID)
Expand All @@ -116,4 +120,75 @@ internal class FriendServiceImplTest : FunSpec({
request.username shouldBe friend.username
}
}

test("친구신청수락_성공") {
val receivedFriend = mockk<Friend>(relaxed = true)
val requestedFriend = mockk<Friend>(relaxed = true)

every {
friendRepository.findByUserIdAndFriendIdAndRequestStatus(
CommonTest.TEST_USER_ID,
CommonTest.TEST_FRIEND_ID,
FriendRequestStatus.RECEIVED,
)
} returns Optional.of(receivedFriend)
every {
friendRepository.findByUserIdAndFriendIdAndRequestStatus(
CommonTest.TEST_FRIEND_ID,
CommonTest.TEST_USER_ID,
FriendRequestStatus.REQUESTED,
)
} returns Optional.of(requestedFriend)

every { receivedFriend.updateRequestStatus(any()) } just Runs
every { requestedFriend.updateRequestStatus(any()) } just Runs

val response =
friendService.acceptFriendRequest(CommonTest.TEST_USER_ID, CommonTest.TEST_FRIEND_ID)
response.friendId shouldBe CommonTest.TEST_FRIEND_ID

verify(exactly = 2) {
friendRepository.findByUserIdAndFriendIdAndRequestStatus(any(), any(), any())
}
}

test("친구신청수락_실패_해당하는 친구신청이 없는 케이스") {
every {
friendRepository.findByUserIdAndFriendIdAndRequestStatus(
CommonTest.TEST_USER_ID,
CommonTest.TEST_FRIEND_ID,
FriendRequestStatus.RECEIVED,
)
} throws ServiceException(UserErrorCode.FRIENDSHIP_ALREADY_EXISTS_OR_NOT_FOUND)

val exception =
shouldThrow<ServiceException> {
friendService.acceptFriendRequest(CommonTest.TEST_USER_ID, CommonTest.TEST_FRIEND_ID)
}
exception.errorCode.message() shouldBe "해당하는 친구신청이 없거나 이미 친구입니다."
}

test("친구신청수락_실패_이미 친구인 케이스") {

every {
friendRepository.findByUserIdAndFriendIdAndRequestStatus(
CommonTest.TEST_USER_ID,
CommonTest.TEST_FRIEND_ID,
FriendRequestStatus.RECEIVED,
)
} returns Optional.empty()
every {
friendRepository.findByUserIdAndFriendIdAndRequestStatus(
CommonTest.TEST_FRIEND_ID,
CommonTest.TEST_USER_ID,
FriendRequestStatus.REQUESTED,
)
} returns Optional.empty()

val exception =
shouldThrow<ServiceException> {
friendService.acceptFriendRequest(CommonTest.TEST_USER_ID, CommonTest.TEST_FRIEND_ID)
}
exception.errorCode.message() shouldBe "해당하는 친구신청이 없거나 이미 친구입니다."
}
})

0 comments on commit 7095dc6

Please sign in to comment.