Tanveer
Tanveer

Reputation: 97

com.jayway.jsonpath.InvalidPathException: Invalid path $myVar.id

In my mockito test case i'm using the below snippet ,

this.mockMvc.perform(get(myURL + myId).param("mock", "false").param("filter","false")).andDo(print())
                .andExpect(status().isOk()).andExpect(jsonPath("$myVar.id", is(SOME_ID)));

at this line i'm getting the below error ,

com.jayway.jsonpath.InvalidPathException: Invalid path $myVar.id
    at com.jayway.jsonpath.internal.PathCompiler.compile(PathCompiler.java:76)
    at com.jayway.jsonpath.JsonPath.<init>(JsonPath.java:98)
    at com.jayway.jsonpath.JsonPath.compile(JsonPath.java:374)
    at org.springframework.test.util.JsonPathExpectationsHelper.<init>(JsonPathExpectationsHelper.java:62)
    at org.springframework.test.web.servlet.result.JsonPathResultMatchers.<init>(JsonPathResultMatchers.java:63)
    at org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath(MockMvcResultMatchers.java:202)
    at com.mypackage.MyTest.testMyMetod(MyTest.java:232)

and FYI,

i'm using the json-path and json-path-assert as a dependency with below version 1.1.0

and i tried the latest version 2.4.0 and also the very old version 0.8.1

But i got the same issue.

Help will be really appreciated.

Upvotes: 2

Views: 6151

Answers (2)

Tanveer
Tanveer

Reputation: 97

I have to use below pattern matcher in jsonPath,

.andExpect(jsonPath("$['myVar'].id", is(SOME_ID)));

Upvotes: 0

talex
talex

Reputation: 20542

It is look like path should be "$.myVar.id" or "myVar.id". According to source of PathCompiler they are equal.

Upvotes: 3

Related Questions