Skip to content

Commit

Permalink
🧪TEST CODE Ski Resorts API
Browse files Browse the repository at this point in the history
  • Loading branch information
jun108059 committed Sep 22, 2024
1 parent 8609ff0 commit 5df59e9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nexters.weski.ski_resort

import io.mockk.every
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*

import com.ninjasquad.springmockk.MockkBean

@WebMvcTest(SkiResortController::class)
class SkiResortControllerTest @Autowired constructor(
private val mockMvc: MockMvc
) {

@MockkBean
lateinit var skiResortService: SkiResortService

@Test
fun `GET api_ski-resorts should return list of ski resorts`() {
// Given
val skiResorts = listOf(
SkiResortDto(1, "스키장 A", ResortStatus.운영중, "2023-12-01", "2024-03-01", 5, 10),
SkiResortDto(2, "스키장 B", ResortStatus.예정, "2023-12-15", null, 0, 8)
)
every { skiResortService.getAllSkiResorts() } returns skiResorts

// When & Then
mockMvc.perform(get("/api/ski-resorts"))
.andExpect(status().isOk)
.andExpect(jsonPath("$[0].name").value("스키장 A"))
.andExpect(jsonPath("$[1].name").value("스키장 B"))
}
}
30 changes: 30 additions & 0 deletions src/test/kotlin/nexters/weski/ski_resort/SkiResortServiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nexters.weski.ski_resort

import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class SkiResortServiceTest {

private val skiResortRepository: SkiResortRepository = mockk()
private val skiResortService = SkiResortService(skiResortRepository)

@Test
fun `getAllSkiResorts should return list of SkiResortDto`() {
// Given
val skiResorts = listOf(
SkiResort(1, "스키장 A", ResortStatus.운영중, null, null, 5, 10),
SkiResort(2, "스키장 B", ResortStatus.예정, null, null, 0, 8)
)
every { skiResortRepository.findAll() } returns skiResorts

// When
val result = skiResortService.getAllSkiResorts()

// Then
assertEquals(2, result.size)
assertEquals("스키장 A", result[0].name)
assertEquals("스키장 B", result[1].name)
}
}

0 comments on commit 5df59e9

Please sign in to comment.