반응형
code:400
message:HTTP Status 400 - Required String parameter 'page' is not present
(JUnit 5.8.2)
원인: 파라미터가 올바르게 들어오지 않았을 때
여러 경우가 있다. 나같은 경우 ↓
테스트 대상 핸들러 메서드(getMemebers())의 파라미터는 @RequestParam 으로 받아주는데
나는 Json으로 변환해서 RequestBody로 넣어주고 있었다.
//수정 전
@Test
void getMembersTest() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("page", "1");
map.put("size", "10");
String content = gson.toJson(map);
mockMvc.perform(get("/v11/members/")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(content)) // 여기
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").isArray());
}
해결 (1) content 대신 param을 사용해주었다.
//수정 후 (1)
@Test
void getMembersTest() throws Exception {
mockMvc.perform(get("/v11/members/")
.accept(MediaType.APPLICATION_JSON)
// .content(content)) 수정
.param("page", "1") // param
.param("size", "10")) // param
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").isArray());
}
해결 (2) params와 MultiValueMap을 사용해준다.
//수정 후 (2)
@Test
void getMembersTest() throws Exception {
// MultiValueMap(params) 생성
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("page", "1");
params.add("size", "10");
mockMvc.perform(get("/v11/members/")
.accept(MediaType.APPLICATION_JSON)
.params(params)) // params
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").isArray());
}
반응형
'공부기록 > SPRING' 카테고리의 다른 글
Spring Security - JWT 인증 적용기 (2) AuthenticationHandler (0) | 2023.02.19 |
---|---|
JWT 토큰 인증 (0) | 2023.02.19 |
Spring Security - JWT 인증 적용기 (1) 로그인 시 사용자 인증, JWT 발급 (0) | 2023.02.17 |
Spring Data JPA | InvalidDataAccessApiUsageException: "detached entity passed to (5) | 2023.01.11 |
For queries with named parameters 에러 (0) | 2022.12.29 |
댓글