2013-11-01 9 views
8

के लिए जेसनपाथ जुनीट से बचने वाला चरित्र मेरे पास एक जेसन फ़ील्ड है जिसे टेम्पलेट.वेलकम.मेल कहा जाता है और मैं एक यूनिट टेस्ट लिख रहा हूं जो जांचता है कि क्या सर्वर सर्वर से उत्तर में मौजूद है, लेकिन मुझे बच नहीं मिल रहा है क्षेत्र के नाम पर बिंदुओं के लिए। अपने परीक्षण के कोड है:डॉट्स

@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 के लिए किसी भी भागने चरित्र को जानते हो?

उत्तर

4

रूप Idapointed out:

उपयोग कोष्ठक और अपने मैदान के चारों ओर उद्धरण। उदाहरण के लिए, अपने क्षेत्र valid.key.with.dot है अगर

[ 'valid.key.with.dot'] के रूप में यह का संदर्भ लें और JsonPath में, कोशिश

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

+0

मेरे पास एक ही विचार थे, चलो देखते हैं कि किसी के पास एक चालाक विचार है .. – daniele

+0

ओह, यही कारण है कि मेरा मार्ग (बच निकले हुए फेंकने के साथ) काम नहीं कर रहा है! – Tom

+0

हां यह संभव है, इडा के उत्तर –

32

उपयोग अपने क्षेत्र के चारों ओर ब्रैकेट और उद्धरण। https://groups.google.com/forum/#!topic/jsonpath/7YvgXWP1_7Y

+0

यह एक अन्य पुस्तकालयों में भी काम करता है जो आंतरिक रूप से जेसनपाथ का उपयोग करते हैं (उदाहरण के लिए आरईएसटी-आश्वासन दिया गया)। धन्यवाद इदा! –

+0

बस एसओएपी यूआई में भी ऐसा किया - जेसनपाथ मैच। अच्छी तरह से काम! – iaforek

3

इन दिनों (उदाहरण के लिए, अपने क्षेत्र valid.key.with.dot

, ['valid.key.with.dot'] के रूप में और JsonPath में यह देखें की कोशिश

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

इस सूत्र यह भी देखें है अगर जैसे 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) 

मैं corr पाया esponding संकेत in this Git issue

संबंधित मुद्दे