-
카테고리
-
세부 분야
백엔드
-
해결 여부
해결됨
test 실행시 org.springframework.http.converter.HttpMessageNotWritableException 500 에러 발생합니다.
20.01.22 06:55 작성 조회수 458
0
package com.devyu.webmvc;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void hello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("안녕"));
}
@Test
public void createUser() throws Exception {
String userJson="{\"userName\":\"devyu\", \"userPassword\":\"min\"}";
mockMvc.perform(post("/create/user")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(userJson))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username",is(equalTo("devyu"))))
.andExpect(jsonPath("$.password",is(equalTo("min"))));
}
}
우선 controller단에 요청하고 @RequestBody와 HttpMessageConverter를 통해 객체에 json값을 주입해주는것 까지는 정상작동 합니다.(해당 controller단에서 user객체를 print() 해보았습니다.) 문제는 객체를 return 해주면 org.springframework.http.converter.HttpMessageNotWritableException 500 에러 발생합니다. 원인이 무엇일까요???
답변을 작성해보세요.
2
백기선
지식공유자2020.01.22
에러 메시지가 좀 더 구체적으로 출력됐을텐데.. 그 메시지를 좀 보여주시면 도움이 될거 같구요. 컨트롤러에서 return한 객체 코드도 보여주세요.
0
puregyu
질문자2020.01.22
return 객체를 보여드리려고 다시 본 순간...
return 객체의 getter와 setter의 접근제어자가 디폴트로 설정되있더군요 ㅠㅠ..
새벽에 정신이 하나도 없었나봐요..답변 감사드립니다! 강의 넘 잘듣고 있습니다 :)
답변 2