복붙노트

[SPRING] 도트를위한 JsonPath JUnit 이스케이프 문자

SPRING

도트를위한 JsonPath JUnit 이스케이프 문자

template.welcome.email라는 json 필드가 있고 그 필드가 서버의 응답에 있는지 검사하지만 필드 이름에 점에 대한 이스케이프를 찾을 수없는 단위 테스트를 작성하고 있습니다. . 내 테스트 코드는 다음과 같습니다.

@Test
public void testEmailTemplates() throws Exception {     
    mockMvc.perform(get("/emailTemplates")
        .contentType(MediaType.APPLICATION_JSON)
        .locale(Locale.UK)
        .accept(MediaType.APPLICATION_JSON))

        .andDo(print())
        .andExpect(status().isOk())

        .andExpect(jsonPath("$.template.welcome.email").exists())

        .andExpect(redirectedUrl(null))
        .andExpect(forwardedUrl(null));
}

그러나 점들은 경로처럼 해석되기 때문에 다음과 같은 예외가 발생합니다.

java.lang.AssertionError: No value for JSON path: $.template.welcome.email, exception: invalid path
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:74)
at org.springframework.test.util.JsonPathExpectationsHelper.exists(JsonPathExpectationsHelper.java:121)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers$3.match(JsonPathResultMatchers.java:77)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
at 

jsonPath의 이스케이프 문자를 아십니까?

해결법

  1. ==============================

    1.아이다가 지적했듯이 :

    아이다가 지적했듯이 :

  2. ==============================

    2.귀하의 필드 주변에 괄호와 따옴표를 사용하십시오. 예를 들어, 필드가 valid.key.with.dot 인 경우

    귀하의 필드 주변에 괄호와 따옴표를 사용하십시오. 예를 들어, 필드가 valid.key.with.dot 인 경우

    그것을 [ 'valid.key.with.dot']라고 쓰고 JsonPath에서 다음과 같이 해보십시오.

    JsonPath.read(jsonString, "$.['valid.key.with.dot']")
    

    이 스레드도 참조하십시오 : https://groups.google.com/forum/#!topic/jsonpath/7YvgXWP1_7Y

  3. ==============================

    3.요즘 (예 : io.rest-assured.json-path : 3.0.1) 표기법은 대괄호가없는 것 같습니다.

    요즘 (예 : io.rest-assured.json-path : 3.0.1) 표기법은 대괄호가없는 것 같습니다.

    // Some Groovy OData V4 $count test
    // Response looks like:
    // { "@odata.count": 2, ... }
    body "'@odata.count'", equalTo(2)
    

    이 Git 문제에서 해당하는 힌트를 찾았습니다.

  4. from https://stackoverflow.com/questions/19726859/jsonpath-junit-escape-character-for-dots by cc-by-sa and MIT license