JsonPath JUnit转义字符点 [英] JsonPath JUnit escape character for dots

查看:689
本文介绍了JsonPath JUnit转义字符点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为template.welcome.email的json字段,我正在编写一个单元测试,以检查服务器的回复中是否存在该字段,但是我找不到名称中的点的转义符领域的. 我测试的代码是:

I have a json field which is called template.welcome.email and I am writing a unit test that checks if that field is present in the reply from the server but I can't find an escape for the dots in the name of the field. The code of my test is :

@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));
}

但是我得到了以下异常,因为点的解释就像路径一样:

But I get the following exception, because the dots are interpreted like paths:

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的任何转义字符吗?

Do you know any escape character for the jsonPath?

推荐答案

Ida

在您的字段中使用方括号和引号.例如,如果您的字段是有效的.key.with.dot

Use brackets and quotes around your field. For example, if your field is valid.key.with.dot

将其引用为['valid.key.with.dot'],然后在JsonPath中尝试

Refer to it as ['valid.key.with.dot'] and in JsonPath, try

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

这篇关于JsonPath JUnit转义字符点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆