title: Asserting list of return items from Spring Controller with mockMvc
body: <p>I've set up a spring boot application to use a Spring MVC controller to return a list of items. I have a spring test that creates a mocked dependency that gets wired in to the Controller, and the controller returns the list of expected mocked items as a JSON array.</p>
  <p>I'm trying to assert that the content is correct simply. I want to assert that the JSON array contains the expected list. I think there's a problem trying to interpret the JSON array as a java.util.List. Is there a way to do this?</p>
  <p>The first and second <code>.andExpect()</code> pass, however, the <code>hasItems()</code> checks do not pass. What can I do so I can just pass in my <code>List&lt;T&gt;</code> and verify it's contained in the JSON? The alternative I can think of is to conver the JSON to my <code>List&lt;T&gt;</code> and verify it using &quot;regular java junit assertions&quot;</p>
  <pre class="lang-java prettyprint-override"><code>public class StudentControllerTest extends AbstractControllerTest {
      @Mock
      private StudentRepository mStudentRepository;
  
      @InjectMocks
      private StudentController mStudentController;
  
      private List&lt;Student&gt; mStudentList;
      
      @Before
      public void setUp() {
          MockitoAnnotations.initMocks(this);
  
          setUp(mStudentController);
  
          // mock the student repository to provide a list of 3 students.
          mStudentList = new ArrayList&lt;&gt;();
          mStudentList.add(new Student(&quot;Egon Spengler&quot;, new Date(), &quot;111-22-3333&quot;));
          mStudentList.add(new Student(&quot;Peter Venkman&quot;, new Date(), &quot;111-22-3334&quot;));
          mStudentList.add(new Student(&quot;Raymond Stantz&quot;, new Date(), &quot;111-22-3336&quot;));
          mStudentList.add(new Student(&quot;Winston Zeddemore&quot;, new Date(), &quot;111-22-3337&quot;));
  
          when(mStudentRepository.getAllStudents()).thenReturn(mStudentList);
      }
      
      @Test
      public void listStudents() throws Exception {
          MvcResult result =
                  mockMvc.perform(get(&quot;/students/list&quot;))
                          .andDo(print())
                          .andExpect(jsonPath(&quot;$&quot;, hasSize(mStudentList.size())))
                          .andExpect(jsonPath(&quot;$.[*].name&quot;, hasItems(&quot;Peter Venkman&quot;, &quot;Egon Spengler&quot;, &quot;Raymond Stantz&quot;, &quot;Winston Zeddemore&quot;)))
  
                          // doesn't work
                          .andExpect(jsonPath(&quot;$.[*]&quot;, hasItems(mStudentList.toArray())))
                          // doesn't work
                          .andExpect(jsonPath(&quot;$.[*]&quot;, hasItems(mStudentList.get(0))))
  
                          .andExpect(status().isOk())
                          .andReturn();
  
  
          String content = result.getResponse().getContentAsString();
          
      }
  }
  </code></pre>