Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 테스트 코드 given-when-then 주석 삭제 및 테스트 가독성 개선 (#22) #23

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions src/test/kotlin/kr/galaxyhub/sc/api/v1/news/NewsControllerV1Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ class NewsControllerV1Test(

describe("GET /api/v1/news/{id}") {
context("유효한 요청이 전달되면") {
it("200 응답과 뉴스의 상세 정보가 조회된다.") {
// given
val id = UUID.randomUUID()
val response = newsDetailResponse(id)
every { newsQueryService.getDetailByIdAndLanguage(id, Language.ENGLISH) } returns response
val id = UUID.randomUUID()
val response = newsDetailResponse(id)
every { newsQueryService.getDetailByIdAndLanguage(id, Language.ENGLISH) } returns response

// when & then
it("200 응답과 뉴스의 상세 정보가 조회된다.") {
mockMvc.docGet("/api/v1/news/{id}", id) {
contentType = MediaType.APPLICATION_JSON
param("language" to Language.ENGLISH)
Expand Down Expand Up @@ -84,12 +82,10 @@ class NewsControllerV1Test(

describe("GET /api/v1/news") {
context("유효한 요청이 전달되면") {
it("200 응답과 모든 뉴스의 정보가 조회된다.") {
// given
val response = listOf(newsResponse())
every { newsQueryService.findAll() } returns response
val response = listOf(newsResponse())
every { newsQueryService.findAll() } returns response

// when & then
it("200 응답과 모든 뉴스의 정보가 조회된다.") {
mockMvc.docGet("/api/v1/news") {
contentType = MediaType.APPLICATION_JSON
}.andExpect {
Expand All @@ -113,12 +109,10 @@ class NewsControllerV1Test(

describe("POST /api/v1/news") {
context("유효한 요청이 전달되면") {
it("201 응답과 뉴스의 식별자가 반환된다.") {
// given
val request = newsCreateRequest()
every { newsCommandService.create(any()) } returns UUID.randomUUID()
val request = newsCreateRequest()
every { newsCommandService.create(any()) } returns UUID.randomUUID()

// when & then
it("201 응답과 뉴스의 식별자가 반환된다.") {
mockMvc.docPost("/api/v1/news") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,44 @@ class NewsCommandServiceTest(
describe("create") {
context("originId에 대해 저장된 뉴스가 없으면") {
it("새로운 뉴스가 생성된다.") {
// given
val command = newsCreateCommand()

// when
val id = withContext(Dispatchers.IO) {
newsCommandService.create(command)
val originId = 1L
val newsId = withContext(Dispatchers.IO) {
newsCommandService.create(newsCreateCommand(originId))
}

// then
val news = newsRepository.findById(id)!!
val news = newsRepository.findById(newsId)!!
news shouldNotBe null
news.supportLanguages shouldContainExactly setOf(Language.KOREAN)
}
}

context("originId에 대해 저장된 뉴스가 있으면") {
it("기존 뉴스에 새로운 언어에 대한 컨텐츠가 추가된다.") {
// given
val existsCommand = newsCreateCommand()
withContext(Dispatchers.IO) {
newsCommandService.create(existsCommand)
}
val originId = 1L
val existsCommand = newsCreateCommand(originId)
withContext(Dispatchers.IO) {
newsCommandService.create(existsCommand)
}

// when
it("기존 뉴스에 새로운 언어에 대한 컨텐츠가 추가된다.") {
val command = existsCommand.copy(language = Language.ENGLISH)
val id = withContext(Dispatchers.IO) {
val newsId = withContext(Dispatchers.IO) {
newsCommandService.create(command)
}

// then
val news = newsRepository.findById(id)!!
val news = newsRepository.findById(newsId)!!
news.supportLanguages shouldContainExactly setOf(Language.KOREAN, Language.ENGLISH)
newsRepository.findAll() shouldHaveSize 1
}
}
}
})

private fun newsCreateCommand() = NewsCreateCommand(
private fun newsCreateCommand(originId: Long) = NewsCreateCommand(
newsType = NewsType.NEWS,
title = "제목",
excerpt = "줄거리",
publishedAt = ZonedDateTime.of(LocalDateTime.parse("2023-12-04T03:53:33"), ZoneId.systemDefault()),
originId = 1,
originId = originId,
originUrl = "https://sc.galaxyhub.kr/",
language = Language.KOREAN,
content = "내용"
Expand Down