-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/test/kotlin/nexters/weski/snow_maker/SnowMakerControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package nexters.weski.snow_maker | ||
|
||
|
||
import com.ninjasquad.springmockk.MockkBean | ||
import io.mockk.Runs | ||
import io.mockk.every | ||
import io.mockk.just | ||
import nexters.weski.common.config.JpaConfig | ||
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.context.annotation.ComponentScan | ||
import org.springframework.context.annotation.FilterType | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.* | ||
|
||
@WebMvcTest(SnowMakerController::class) | ||
@ComponentScan( | ||
excludeFilters = [ComponentScan.Filter( | ||
type = FilterType.ASSIGNABLE_TYPE, | ||
classes = [JpaConfig::class] | ||
)] | ||
) | ||
class SnowMakerControllerTest @Autowired constructor( | ||
private val mockMvc: MockMvc | ||
) { | ||
|
||
@MockkBean | ||
lateinit var snowMakerService: SnowMakerService | ||
|
||
@Test | ||
fun `GET api_snow-maker_resortId should return snow Maker data`() { | ||
// Given | ||
val resortId = 1L | ||
val snowMakerDto = SnowMakerDto(resortId, 100, 80, "์ข์") | ||
every { snowMakerService.getSnowMaker(resortId) } returns snowMakerDto | ||
|
||
// When & Then | ||
mockMvc.perform(get("/api/snow-maker/$resortId")) | ||
.andExpect(status().isOk) | ||
.andExpect(jsonPath("$.totalVotes").value(100)) | ||
.andExpect(jsonPath("$.status").value("์ข์")) | ||
} | ||
|
||
@Test | ||
fun `POST api_snow-maker_resortId_vote should vote successfully`() { | ||
// Given | ||
val resortId = 1L | ||
every { snowMakerService.voteSnowMaker(any(), any()) } just Runs | ||
|
||
// When & Then | ||
mockMvc.perform(post("/api/snow-maker/$resortId/vote") | ||
.param("isPositive", "true")) | ||
.andExpect(status().isOk) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/kotlin/nexters/weski/snow_maker/SnowMakerServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package nexters.weski.snow_maker | ||
|
||
|
||
import io.mockk.* | ||
import nexters.weski.ski_resort.ResortStatus | ||
import nexters.weski.ski_resort.SkiResort | ||
import nexters.weski.ski_resort.SkiResortRepository | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import java.util.Optional | ||
|
||
class SnowMakerServiceTest { | ||
|
||
private val snowMakerVoteRepository: SnowMakerVoteRepository = mockk(relaxed = true) | ||
private val skiResortRepository: SkiResortRepository = mockk() | ||
private val snowMakerService = SnowMakerService(snowMakerVoteRepository, skiResortRepository) | ||
|
||
@Test | ||
fun `getSnowMaker should return SnowMakerDto`() { | ||
// Given | ||
val resortId = 1L | ||
every { snowMakerVoteRepository.countBySkiResortResortId(resortId) } returns 100 | ||
every { snowMakerVoteRepository.countBySkiResortResortIdAndIsPositive(resortId, true) } returns 80 | ||
|
||
// When | ||
val result = snowMakerService.getSnowMaker(resortId) | ||
|
||
// Then | ||
assertEquals(100, result.totalVotes) | ||
assertEquals(80, result.positiveVotes) | ||
assertEquals("์ข์", result.status) | ||
} | ||
|
||
@Test | ||
fun `voteSnowMaker should save vote`() { | ||
// Given | ||
val resortId = 1L | ||
val isPositive = true | ||
val skiResort = SkiResort(resortId, "์คํค์ฅ A", ResortStatus.์ด์์ค, null, null, 5, 10) | ||
val snowMakerVote = SnowMakerVote( | ||
isPositive = isPositive, | ||
skiResort = skiResort | ||
) | ||
every { skiResortRepository.findById(resortId) } returns Optional.of(skiResort) | ||
every { snowMakerVoteRepository.save(any()) } returns snowMakerVote | ||
|
||
// When | ||
snowMakerService.voteSnowMaker(resortId, isPositive) | ||
|
||
// Then | ||
verify { snowMakerVoteRepository.save(any()) } | ||
} | ||
} |