title: MockMvc and Mockito - java.lang.AssertionError: JSON path "$" Expected: a collection with size <2> but: collection size was <0>
body: <p>I have no idea why I am getting this error here. Any thoughts? </p>
  
  <p>Here is my repository code</p>
  
  <pre><code>package movieweb.movies.repository;
  
  import movieweb.movies.models.Movies;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface MoviesRepository extends CrudRepository&lt;Movies, Integer&gt; {
  }
  </code></pre>
  
  <p>Here is my Controller code.</p>
  
  <pre><code>package movieweb.movies.controllers;
  import movieweb.movies.models.Movies;
  import movieweb.movies.models.UserMovies;
  import movieweb.movies.repository.MoviesRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.web.bind.annotation.*;
  
  import java.util.List;
  
  import org.springframework.web.client.RestTemplate;
  import org.springframework.web.server.ResponseStatusException;
  
  @RestController
  public class MoviesController {
  
      @Autowired
      private MoviesRepository moviesRepository;
      @Autowired
      private RestTemplate restTemplate;
  
      @CrossOrigin
      @GetMapping(path = "/movies")
      public  List&lt;Movies&gt; movies(){
          List&lt;Movies&gt; allMovies = (List&lt;Movies&gt;) moviesRepository.findAll();
          if (!allMovies.isEmpty()){
              return allMovies;
          } else {
              throw new ResponseStatusException(
                      HttpStatus.NOT_FOUND, "Movies not found"
              );
          }
  
  
      }
  
  //    @CrossOrigin
  //    @RequestMapping(path = "movies/user/{id}")
  //    public List&lt;Movies&gt; movie(@PathVariable("id") int id){
  //        return this.movies().stream().map(movie -&gt; {
  //            Users[] user = restTemplate.getForObject("http://127.0.0.1:8082/users/" + id, Users[].class);
  //            return new Movies(movie.getMovieId(), movie.getMovieName(), "Description");
  //        })
  //                .collect(Collectors.toList());
  //    }
  
      @CrossOrigin
      @GetMapping(path="/movie/{id}")
      public  Movies getMovie(@PathVariable Integer id){
          return moviesRepository.findById(id)
                  .orElseThrow(() -&gt; new ResponseStatusException(
                  HttpStatus.NOT_FOUND, "Movie not found"
          ) );
      }
  
      @CrossOrigin
      @DeleteMapping("/movie/delete/{id}")
      void deleteMovie(@PathVariable Integer id) {
          moviesRepository.deleteById(id);
      }
  
      @CrossOrigin
      @PutMapping("/movie/update/{id}")
      Movies updateMovie(@RequestBody Movies updateMovie, @PathVariable Integer id) {
  
          return moviesRepository.findById(id)
                  .map(Movies -&gt; {
                      Movies.setMovieName(updateMovie.getMovieName());
                      Movies.setMovieDescription(updateMovie.getMovieDescription());
                      return moviesRepository.save(Movies);
                  })
                  .orElseGet(() -&gt; {
                      updateMovie.setMovieId(id);
                      return moviesRepository.save(updateMovie);
                  });
      }
  
      @CrossOrigin
      @PostMapping(path="/newMovie")
      public Movies addNewMovie (@RequestBody Movies data) {
         return moviesRepository.save(data);
      }
  
  
  }
  </code></pre>
  
  <p>and here is my test (<strong>updated</strong>) </p>
  
  <pre><code>package movieweb.movies;
  import movieweb.movies.controllers.MoviesController;
  import movieweb.movies.models.Movies;
  import movieweb.movies.repository.MoviesRepository;
  import org.junit.jupiter.api.Test;
  import org.mockito.Mock;
  import org.mockito.Mockito;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.boot.test.mock.mockito.MockBean;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  import org.springframework.web.context.WebApplicationContext;
  import org.testng.annotations.BeforeMethod;
  
  import java.util.ArrayList;
  
  import static org.hamcrest.Matchers.is;
  import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
  import static org.mockito.Mockito.times;
  import static org.mockito.Mockito.when;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  @SpringBootTest
  @AutoConfigureMockMvc
  
  class MovieApplicationTests {
  
      @Autowired
      MockMvc mockMvc;
  
      @MockBean
      MoviesRepository moviesRepository;
  
      @MockBean
      MoviesController moviesController;
  
  
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      @BeforeMethod
      public void init() {
          mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  
      }
  
      @Test
      void getAllMovies() throws Exception{
  
          ArrayList&lt;Movies&gt; moviesList = new ArrayList&lt;Movies&gt;();
          moviesList.add(new Movies(1, "Star Wars", "A New Hope"));
          moviesList.add(new Movies(2, "Star Wars", "Empire Strikes Back"));
  
              when(moviesRepository.findAll()).thenReturn(moviesList);
          mockMvc.perform(get("/movies"))
                  .andExpect(status().isOk())
                  .andExpect(jsonPath("$", hasSize(2)))
                  .andExpect(jsonPath("$[0].movieName", is("Star Wars")))
                  .andExpect(jsonPath("$[0].movieDescription", is("A New Hope")))
                  .andExpect(jsonPath("$[1].movieName", is("Star Wars")))
                  .andExpect(jsonPath("$[1].movieDescription", is("Empire Strikes Back")));
  
  
          Mockito.verify(moviesRepository, times(1)).findAll();
      }
  }
  </code></pre>
  
  <p>The error I am getting in my stacktrace is the following....</p>
  
  <pre><code>java.lang.AssertionError: JSON path "$"
  Expected: a collection with size &lt;2&gt;
       but: collection size was &lt;0&gt;
  </code></pre>
  
  <p>Why is it zero and not the 2 items I created in my movieList ArrayList?</p>
  
  <p>I'm guessing its something to do with my configuration?</p>