Skip to content

Commit

Permalink
test: 알림 목록 조회 API Test
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed May 5, 2024
1 parent f53c343 commit 5aa24ae
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package site.katchup.springboot.controller

import io.kotest.assertions.print.print
import io.kotest.core.spec.style.FunSpec
import io.mockk.every
import io.mockk.mockk
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import site.katchup.springboot.dto.notification.response.NotificationListResponse
import site.katchup.springboot.dto.notification.response.NotificationResponse
import site.katchup.springboot.service.NotificationService
import java.time.LocalDateTime

@WebMvcTest
class NotificationControllerTest() : FunSpec() {
private val notificationService = mockk<NotificationService>(relaxed = true)
private val mockMvc = MockMvcBuilders.standaloneSetup(
NotificationController(
notificationService = notificationService,
),
).build()

init {
test("[GET /notifications] 알림 조회 API Test") {
// given
every {
notificationService.getNotifications(any())
} returns NotificationListResponse.of(
listOf(
NotificationResponse(
id = 1L,
title = "test 알림",
content = "안녕하세요",
isRead = false,
createdAt = LocalDateTime.now(),
),
NotificationResponse(
id = 2L,
title = "test 알림2",
content = "안녕하세요",
isRead = false,
createdAt = LocalDateTime.now(),
),
),
)
}

this.mockMvc.perform(
get("/api/v1/notifications")
.param("memberId", "1"),
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.message").value("알림 목록 조회 성공"))
.print()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package site.katchup.springboot.repository

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import site.katchup.springboot.entity.Member
import site.katchup.springboot.entity.Notification
import site.katchup.springboot.entity.SocialType

@RepositoryTest
class NotificationRepositoryTest(
private val notificationRepository: NotificationRepository,
private val memberRepository: MemberRepository,
) : FunSpec() {

init {
test("사용자의 알림 목록을 최신순으로 조회할 수 있다.") {
// given
val member = Member(
name = "name",
nickname = "nickname",
socialId = "socialId",
email = "unan@katchup.site",
socialType = SocialType.GOOGLE,
profileMemo = "lineMemo",
workSpaceId = 1,
profileImage = "https://katchup.site/image/1",
)
val savedMember = memberRepository.save(member)
val notification1 = Notification(
title = "title1",
content = "content1",
isRead = false,
memberId = savedMember.id!!,
)
val notification2 = Notification(
title = "title2",
content = "content2",
isRead = false,
memberId = savedMember.id!!,
)
val notification3 = Notification(
title = "title3",
content = "content3",
isRead = false,
memberId = savedMember.id!!,
)
notification1.createdAt = notification1.createdAt.plusDays(1)
notification2.createdAt = notification2.createdAt.plusDays(2)
notification3.createdAt = notification3.createdAt.plusDays(3)
notificationRepository.saveAll(listOf(notification1, notification2, notification3))

// when
val notifications = notificationRepository.findAllByMemberIdOrderByCreatedAtDesc(savedMember.id!!)

// then
notifications.size shouldBe 3
notifications[0].title shouldBe "title3"
notifications[0].content shouldBe "content3"

notifications[1].title shouldBe "title2"
notifications[1].content shouldBe "content2"

notifications[2].title shouldBe "title1"
notifications[2].content shouldBe "content1"
}
}
}

0 comments on commit 5aa24ae

Please sign in to comment.