오늘 해야할 일은 테스트 코드를 완성하고 뷰에 데이터를 뿌리는일이다.
첫번째로 post 로 통신하고 파라미터를 @RequestBody로 받는 reserveOneCourse 메소드에 대한 테스트 코드를 작성해보았다.
@Test
public void reserveOneCourse() throws Exception {
String content = objectMapper.writeValueAsString(new CourseReservationDto((1L),1,1));
mockMvc.perform(post("/reservation/1")
.content(content))
.andExpect(status().isOk())
.andDo(print());
CourseMembership vo = courseMembershipRepository.getOne(1); // 저장 후 CourseMembershipRespository에서 1번 Course 을 가져온다.
assertThat(vo.getCrsId() == 1L , is(true));
}
실행 결과는 다음과 같다.
Resolved Exception:
Type = org.springframework.web.HttpMediaTypeNotSupportedException
MockHttpServletResponse:
Status = 415
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Accept:"application/hal+json, application/json, application/octet-stream, application/xml, application/x-www-form-urlencoded, application/*+json, text/plain, text/xml, application/*+xml, multipart/form-data, multipart/mixed, */*"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :415
HTTP Response 값이 정상(200)이 아니라 Unsupported Media Type (415) 로 응답을 주었다.
https://www.whatap.io/ko/blog/40/
HTTP 상태 코드 정리 | 와탭 블로그
HTTP 응답 상태 코드의 목록을 정리하여 소개합니다. 클라이언트의 요청에 따라 어떻게 서버가 응답하는지 알아봅시다.
www.whatap.io
@PostMapping("/{mmbrshpId}")
public ResponseEntity<CourseReservationDto> reserveOneCourse(
@RequestBody CourseReservationDto courseReservationDto){
CourseReservation courseReservation = courseReservationService.saveCrsSrv(courseReservationDto);
WebMvcLinkBuilder mvcLinkBuilder = linkTo(CourseReservationController.class).slash(courseReservation.getCrsId());
return ResponseEntity.created(mvcLinkBuilder.toUri()).body(modelMapper.map(courseReservation, CourseReservationDto.class));
}
위에서처럼 post서비스에서 @RequestBody로 받아오는 파라미터들의 contentType이 Application/JSON의 형식이기 때문에 mockMvc.perform시 content-type을 지정해주어야 한다.
https://hbesthee.tistory.com/45
HTTP Content-Type 정리.
언어에 따른 선언 방식 ASP <% Response.ContentType = "text/html" %> JSP <%@ page contentType="text/html" %> PHP <?PHP header("Content-Type:text/html"); ?> Perl print "Content-type: text/html\n\n"; Co..
hbesthee.tistory.com
아래와 같이 테스트 코드에 MediaType을 지정해주었고, status 코드도 isOk()에서 is2xxSuccessful로 변경해주었다.
mockMvc.perform(post("/reservation/1")
.content(content)
.contentType(MediaType.APPLICATION_JSON) // #1. contentType 지정
)
.andExpect(status().is2xxSuccessful()) // #2. isOk() > is2xxSuccessful()로 변경
.andDo(print());
put이나 post 이후 성공적으로 객체가 생성되었다는 응닶값은 200이 아니라 201이기 때문이다.

'개인 프로젝트 > 예약 시스템 개발하기' 카테고리의 다른 글
| 예약 시스템 개발하기 06. EmbeddedId와 ConverterNotFoundException (0) | 2022.03.12 |
|---|---|
| 예약 시스템 개발하기 04. JPA 쿼리 메소드와 JPQL , CharacterEncodingFilter (0) | 2022.03.06 |
| 예약 시스템 개발하기 03. axios 통신과 CORS 정책 (0) | 2022.02.02 |
| 예약 시스템 개발하기 02. Router (0) | 2022.01.31 |
| 예약 시스템 개발하기 01. Vuetify 설치 및 프로젝트 구조 (0) | 2022.01.31 |