본문 바로가기
728x90

전체 글86

ViewResolver Accept header값에 따라 응답이 달라짐 > 해당하는 view에 맞게 Return xml로 응답을 받고 싶은 경우 > xpath사용 예시 @RunWith(SpringRunner.class) @WebMvcTest(UserController.class) public class UserControllerTest { @Autowired MockMvc mockMvc; @Test public void createUser_XML() throws Exception { String userJson = "{\"username\":\"sangguk\", \"password\":\"1234\"}"; mockMvc.perform(post("/users/create") .contentType(MediaType.APPLIC.. 2022. 2. 27.
HttpMediaTypeNotAcceptableException MockHttpServletRequest: HTTP Method = POST Request URI = /users/create Parameters = {} Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/xml", Content-Length:"41"] Body = {"username":"sangguk", "password":"1234"} Session Attrs = {} Handler: Type = com.example.springbootwebmvc.user.UserController Method = com.example.springbootwebmvc.user.UserController#create(User) As.. 2022. 2. 27.
HttpMessageConverters : http요청 본문을 객체로 변환하거나, 객체를 http응답 본문으로 변경할 때 사용 @ResponseBody : 해당 객체의 converter를 사용해서 응답 return @RequestBody : 해당 객체의 converter를 사용해서 객체로 변환 @RestController public class UserController { @PostMapping("/userinfo") public @ResponseBody User postUserInformation(@RequestBody User user){ return user; } } * @ResponseBody은 @RestController를 사용하는 경우 생략 가능 Test 코드 @RunWith(SpringRunner.class) @WebMvcTes.. 2022. 2. 20.
Spring boot 단위 테스트 단위 테스트 (Slice Test) > 레이어별로 잘라서 테스트하고 싶은 경우 사용 방법 (이전의 예제 사용) 참고 : https://developing-countries.tistory.com/57 의 3번 예제 @WebMvcTest > web관련 Bean만 생성 > Service들을 MockBean으로 생성해줘야함 ex) @Controller, @ControllerAdvice, @JsonComponent @RunWith(SpringRunner.class) @WebMvcTest(SampleController.class) class SampleControllerTest { @Autowired MockMvc mockMvc; @MockBean SampleService mockSampleService; @Tes.. 2022. 2. 20.
728x90