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

查看:14
本文介绍了点的 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?

推荐答案

As Ida 指出:

在您的字段周围使用括号和引号.例如,如果您的字段是 valid.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天全站免登陆