simDev1234
심플하고 차분하게
simDev1234
전체 방문자
오늘
어제
  • 분류 전체보기
    • Computer Science
      • Basic Math
      • Data Structure
      • Algorithm
      • Database
      • OS
    • Language
      • Java
      • Kotlin
      • SQL
    • Framework
      • Spring
      • Orm&Mapper
      • 프로젝트로 스프링 이해하기
      • 스프링 라이브러리
    • Infra
      • Cloud
      • Docker
      • Redis
      • AWS, Azure
      • Device
    • Etc
      • CleanCoding
    • Git,Github

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 컨트롤러
  • scanner #next() #nextLine()
  • 스프링
  • 자바프로그램
  • controllerTest
  • 참조타입
  • 자바
  • 404
  • JVM메모리구조
  • null
  • 자바메모리구조
  • 자바프로그래밍
  • 참조변수

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
simDev1234

심플하고 차분하게

Framework/스프링 라이브러리

[TEST] Controller Test에서 ObjectMapper 사용하기

2022. 9. 20. 23:11

|  ObjectMapper로 Json으로 파싱

- 방법

  (1) 테스트 클래스 안에 @Autowired로 test-container의 ObjectMapper를 주입한다.

  (2) 테스트 메소드 안에 contentType과 content를 지정한다.

       contentType에 MediaType.APPLICATION_JSON을 지정하고,

       content에 object -> json형식의 string으로 반환하는 objectMapper의 writeValueAsString(Object value)를 쓴다.

@Autowired
ObjectMapper objectMapper;

@Test
void createXX() throws Exception {
    // given 생략
    // then 
    mockMvc.perform(post("/path")
        .contentType(MediaType.APPLICATION_JSON)
        .content(objectMapper.writeValueAsString(
        	new CreateXX.Request(1L, 1234L)
        ))
    );
}

(3) 경로 확인하기

// 객체
jsonPath("$.userId").value(1L)

// 배열
jsonPath("$[0].userId").value(1L)
jsonPath("$[1].userId").value(2L)
jsonPath("$[2].userId").value(3L)

- Json-Path 작성 방법 상세

https://github.com/json-path/JsonPath

 

GitHub - json-path/JsonPath: Java JsonPath implementation

Java JsonPath implementation. Contribute to json-path/JsonPath development by creating an account on GitHub.

github.com

 

[ 참고 및 출처 ]

부트캠프 강의 내용

https://joojimin.tistory.com/52

'Framework > 스프링 라이브러리' 카테고리의 다른 글

[스프링 시큐러티] JWT 토큰 + 스프링 시큐러티  (0) 2022.11.18
[Scheduler] 스케줄러 사용하기 (feat. 쓰레드, 쓰레드풀)  (0) 2022.11.11
[스프링 시큐러티] 스프링 시큐러티 자료 모음 (수정중)  (0) 2022.10.29
[TEST] TDD 방식에 대한 자료 모음 (수정중)  (0) 2022.10.28
[JSON 파싱] ObjectMapper, Simple-json  (0) 2022.10.24
    'Framework/스프링 라이브러리' 카테고리의 다른 글
    • [Scheduler] 스케줄러 사용하기 (feat. 쓰레드, 쓰레드풀)
    • [스프링 시큐러티] 스프링 시큐러티 자료 모음 (수정중)
    • [TEST] TDD 방식에 대한 자료 모음 (수정중)
    • [JSON 파싱] ObjectMapper, Simple-json
    simDev1234
    simDev1234
    TIL용 블로그. * 저작권 이슈가 있는 부분이 있다면 댓글 부탁드립니다.

    티스토리툴바