I am testing an endpoint which returns a list of objects. I am currently testing each element in sequence as per the code below. The code is abbreviated for clarity.
There may be many elements and many attributes. This code can get long and ugly
public void testXxxOK() throws URISyntaxException, Exception {
when(thisCall).thenReturn(mockThis());
when(thatCall).thenReturn(mockThat());
mockMvc
.perform(get(relativePath(
"/getStuffRestEndPoint?argument1=123&argument2=01-11-2001%200101Z"))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andExpect(content().contentType(APPLICATIONJSON))
.andExpect(jsonPath("$.dataList", hasSize(3)))
.andExpect(jsonPath("$.dataList[0].parameter1", is(0)))
.andExpect(jsonPath("$.dataList[0].parameter2", is("name0")))
.andExpect(jsonPath("$.dataList[1].parameter1", is(1)))
.andExpect(jsonPath("$.dataList[1].parameter2", is("name0")))
.andExpect(jsonPath("$.dataList[2].parameter1", is(2)))
.andExpect(jsonPath("$.dataList[2].parameter2", is("name0")))
}
Can I wrap a loop around the .andExpect() calls so that I have only 1 list of parameters? This is what I mean. How do I do this?
for (i=0; i<size; i++)
{
.andExpect(jsonPath("$.dataList[i].parameter1", is(i)))
.andExpect(jsonPath("$.dataList[i].parameter2", is("name0")))
}