I have a JSON response looking like this
{
"stockId": 1,
"stockName": "SPOT",
"stockValue": 150.00
}
I must ensure that "stockValue" is greater than 0. I tried doing this:
.andExpect(jsonPath("$.stockValue"), Matchers.greaterThan(0.00));
But that produces me a compile time error of this:
'andExpect(org.springframework.test.web.servlet.ResultMatcher)' in 'org.springframework.test.web.servlet.ResultActions' cannot be applied to '(org.springframework.test.web.servlet.result.JsonPathResultMatchers, org.hamcrest.Matcher<java.lang.Double>)'
I searched around, and I tried this solution which doesn't work:
.andExpect(jsonPath("$.stockValue"), Matchers.everyItem(Matchers.greaterThan(0.00)));
Because it produces this compile time error:
'andExpect(org.springframework.test.web.servlet.ResultMatcher)' in 'org.springframework.test.web.servlet.ResultActions' cannot be applied to '(org.springframework.test.web.servlet.result.JsonPathResultMatchers, org.hamcrest.Matcher<java.lang.Iterable<? extends java.lang.Double>>)'
I want to ensure that stockValue is indeed more than 0.00. How would I do that?
This is my entire test function below:
@Test
void getStockDetail200() throws Exception {
// Perform the GET request
String endpoint = MessageFormat.format("{0}?symbol=SPOT&{1}",
Utils.BASE_URL,
Utils.TOKEN);
mockMvc.perform(get(endpoint)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.stockName", Matchers.is("SPOT")))
.andExpect(jsonPath("$.stockValue"), Matchers.greaterThan(0.00));
}
CodePudding user response:
I think you're going to spot this yourself for sure:
jsonPath("$.stockName", Matchers.is("SPOT"))
vs.
jsonPath("$.stockValue"), Matchers.greaterThan(0.00)