Skip to content

Commit

Permalink
Write test codes of GoalGifticonController
Browse files Browse the repository at this point in the history
  • Loading branch information
mkSpace committed Apr 22, 2024
1 parent 5ccb645 commit 4ba9d9e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import org.hamcrest.core.IsNull.nullValue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.Mockito.*
import org.springframework.http.MediaType
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
Expand All @@ -28,9 +26,9 @@ class BettingControllerTest : ControllerTestSupport() {
mockMvc
.perform(
post("/v1/betting")
.with(csrf())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.withCsrf()
.writeRequestAsContent(request)
.contentTypeAsJson()
)
.andDo(::print)
.andExpect(status().isCreated())
Expand All @@ -48,9 +46,9 @@ class BettingControllerTest : ControllerTestSupport() {
mockMvc
.perform(
post("/v1/betting")
.with(csrf())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.withCsrf()
.writeRequestAsContent(request)
.contentTypeAsJson()
)
.andDo(::print)
.andExpect(status().isBadRequest)
Expand Down Expand Up @@ -87,9 +85,9 @@ class BettingControllerTest : ControllerTestSupport() {
mockMvc
.perform(
put("/v1/betting")
.with(csrf())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.withCsrf()
.writeRequestAsContent(request)
.contentTypeAsJson()
)
.andDo(::print)
.andExpect(status().isOk)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.whatever.raisedragon.controller.goalgifticon

import com.whatever.raisedragon.ControllerTestSupport
import com.whatever.raisedragon.security.WithCustomUser
import org.hamcrest.core.IsNull.nullValue
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@WithCustomUser(id = 1L, nickname = "User")
class GoalGifticonControllerTest : ControllerTestSupport() {

@DisplayName("GoalGifticon을 생성한다.")
@Test
fun create() {
// given
val gifticonUrl = "www.sample.com/gifticon"
val request = GoalGifticonCreateRequest(goalId = 1L, gifticonURL = gifticonUrl)

// when // then
mockMvc
.perform(
post("/v1/goal-gifticon")
.withCsrf()
.writeRequestAsContent(request)
.contentTypeAsJson()
)
.andDo(::print)
.andExpect(status().isCreated)
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.errorResponse").value(nullValue()))
}

@DisplayName("다짐 내 GoalGifticon을 조회한다.")
@Test
fun retrieve() {
// given
val goalId = 1L

// when // then
mockMvc
.perform(
get("/v1/goal-gifticon/$goalId")
)
.andDo(::print)
.andExpect(status().isOk)
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.errorResponse").value(nullValue()))
}

@DisplayName("다짐 내 기프티콘을 수정한다.")
@Test
fun update() {
// given
val gifticonUrl = "www.sample.com/updated-gifticon"
val request = GoalGifticonRequest(goalId = 1L, gifticonURL = gifticonUrl)

// when // then
mockMvc
.perform(
post("/v1/goal-gifticon")
.withCsrf()
.writeRequestAsContent(request)
.contentTypeAsJson()
)
.andDo(::print)
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.errorResponse").value(nullValue()))
}
}

0 comments on commit 4ba9d9e

Please sign in to comment.