Skip to content

Commit

Permalink
✅ Test: 응답 형식 테스트 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
loveysuby committed Jul 5, 2024
1 parent 6cdf7c9 commit 8643b09
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package slvtwn.khu.toyouserver.response;

import slvtwn.khu.toyouserver.common.PageInfoResponse;

public record PageableDto(
Long id,
String name,
PageInfoResponse pageInfo) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package slvtwn.khu.toyouserver.response;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/test")
@RestController
public class ResponseController {

private final ResponseService responseService;

public ResponseController(ResponseService responseService) {this.responseService = responseService;}

@GetMapping
public PageableDto health() {
return responseService.health();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package slvtwn.khu.toyouserver.response;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import slvtwn.khu.toyouserver.common.PageInfoResponse;

@AutoConfigureMockMvc
@SpringBootTest
public class ResponseControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private ResponseService responseService;

@DisplayName("서버 응답 테스트를 실행한다.")
@Test
public void 응답_테스트() throws Exception {
PageableDto expect = new PageableDto(1L, "test", PageInfoResponse.of(1, 1, 1));
given(responseService.health())
.willReturn(expect);
mockMvc.perform(get("/test"))
.andExpect(status().isOk())
.andExpect(result -> {
String contentAsString = result.getResponse().getContentAsString();
System.out.println(contentAsString);
});
}
}
13 changes: 13 additions & 0 deletions src/test/java/slvtwn/khu/toyouserver/response/ResponseService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package slvtwn.khu.toyouserver.response;

import org.springframework.stereotype.Service;
import slvtwn.khu.toyouserver.common.PageInfoResponse;

@Service
public class ResponseService {

public PageableDto health() {
PageInfoResponse pageInfo = PageInfoResponse.of(1, 1, 1);
return new PageableDto(1L, "health", pageInfo);
}
}

0 comments on commit 8643b09

Please sign in to comment.