Reputation: 167
I am doing a controller test and am testing to see if the returned json values are correct. this is my json{
"firstName": "Bob",
"lastName": "Jones",
"phoneNumber": "0123456789",
"jobTitle": "Software Developer",
"companyName": "BFS",
"industry": {
"name": "legal"
},
"companySize": {
"noOfEmployees": "1_to_10"
},
"accounts": [
{
"accountName": "TestAccount1",
"accountNo": "123",
"accountType": "PREPAY",
"creditBalance": "25.00",
"creditLimit": "500.00",
"concurrentSubscriptionLimit": "10",
"hitsPerSubscriptionItemLimit": "20",
"hourlyRequestLimit": "30"
}
]
}
and my current test looks like this: mockMvc.perform(request)
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.firstName", is("Bob")))
.andExpect(jsonPath("$.lastName", is("Jones")))
.andExpect(jsonPath("$.phoneNumber", is("0123456789")))
.andExpect(jsonPath("$.jobTitle", is("Software Developer")))
.andExpect(jsonPath("$.companyName", is("BFS")))
.andExpect(jsonPath("$.industry.name", is("legal")))
.andExpect(jsonPath("$.companySize.noOfEmployees", is("1_to_10")))
.andReturn();
}
My questions is, how would I write the jsonPath for accountName in the accounts field? Thanks.
Upvotes: 0
Views: 152
Reputation: 840
To access to the accountName value, you should use this expression :
$.accounts[0].accountName
You can also get all the accountName with this expression :
$.accounts[*].accountName
Hope it helps !
Upvotes: 1