forked from woowacourse/jwp-racingcar
-
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
1 parent
33df83e
commit f1cc1cc
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
web/src/test/java/racingcar/controller/RacingCarControllerSliceTest.java
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,86 @@ | ||
package racingcar.controller; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
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.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import racingcar.domain.RacingGame; | ||
import racingcar.dto.GameRequest; | ||
import racingcar.dto.GameResponse; | ||
import racingcar.service.AddRaceService; | ||
import racingcar.service.FindRaceService; | ||
|
||
@WebMvcTest | ||
class RacingCarControllerSliceTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private AddRaceService addRaceService; | ||
|
||
@MockBean | ||
private FindRaceService findRaceService; | ||
|
||
@Test | ||
void 게임_진행하는_테스트() throws Exception { | ||
//given | ||
final GameRequest gameRequest = new GameRequest(List.of("브리", "토미", "브라운"), 10); | ||
final String request = objectMapper.writeValueAsString(gameRequest); | ||
final GameResponse gameResponse = new GameResponse("브리,토미,브라운", List.of( | ||
new racingcar.dto.CarResponse("브리", 5), | ||
new racingcar.dto.CarResponse("토미", 5), | ||
new racingcar.dto.CarResponse("브라운", 5) | ||
)); | ||
final String response = objectMapper.writeValueAsString(gameResponse); | ||
given(addRaceService.addRace(any(List.class), any(int.class))).willReturn(new RacingGame(1, List.of( | ||
new racingcar.domain.Car("브리", 5), | ||
new racingcar.domain.Car("토미", 5), | ||
new racingcar.domain.Car("브라운", 5) | ||
), 10)); | ||
|
||
//expect | ||
mockMvc.perform(post("/plays") | ||
.contentType(MediaType.APPLICATION_JSON_VALUE) | ||
.content(request)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(response)); | ||
} | ||
|
||
@Test | ||
void 게임_조회_테스트() throws Exception { | ||
//given | ||
final List<GameResponse> gameResponse = List.of(new GameResponse("브리,토미,브라운", List.of( | ||
new racingcar.dto.CarResponse("브리", 5), | ||
new racingcar.dto.CarResponse("토미", 5), | ||
new racingcar.dto.CarResponse("브라운", 5) | ||
))); | ||
final String response = objectMapper.writeValueAsString(gameResponse); | ||
given(findRaceService.findAllRace()).willReturn(List.of( | ||
new RacingGame(1, List.of( | ||
new racingcar.domain.Car("브리", 5), | ||
new racingcar.domain.Car("토미", 5), | ||
new racingcar.domain.Car("브라운", 5) | ||
), 10) | ||
)); | ||
|
||
//expect | ||
mockMvc.perform(get("/plays")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(response)); | ||
} | ||
} |