[Spring Boot] MockMvc JSON Array Test

JSON Response Body

{
  "greetingStrArr":
    [
        "hello",
        "world"
    ]
}

Test Class (With MockMvc)

  • MockMvc -> package org.springframework.test.web.servlet;
@SpringBootTest
@AutoConfigureMockMvc
public class HttpRequestTestMockMvc {
  @Autowired
  private MockMvc mockMvc;
  
  @Autowired
  private HttpRequestController httpRequestController;

  @Test
  public void getRequestTestMockMvc() throws Exception {
    assertThat(httpRequestController).isNotNull();

    this.mockMvc.perform(get("/")
      .param("greeting", "World"))
      .andDo(print())
      .andExpect(status().isOk())
      .andExpect(jsonPath("$.greetingStrArr", Matchers.containsInAnyOrder(
          Stream.of(
              "Hello",
              "World"
          ).map(String::toLowerCase).toArray()
      )));
  }
}

Comments