title: How can I give a List of objects as parameter with MockMvcBuilder
body: <p>In my Spring Boot project, i have a controller called AdminGameController which methods I want to test using Springs MockMvcBuilder. This works fine for methods which handles the GET request, but for the POST methods, a Game object is needed as parameter, see the handler method: </p>
  
  <pre><code>@RequestMapping(value = "/admin/game/save", method = RequestMethod.POST)
  public String saveOrUpdate(@Valid Game game,
                     BindingResult bindingResult,
                     Model model,
                     RedirectAttributes redirectAttributes) {
  
      if (bindingResult.hasErrors()){
          model.addAttribute("message","Please fill in all necessary fields");
          addAttributes(model);
          return (game.getId()==0) ?
                     "admin/games/newGameForm" : "admin/games/editGameForm";
      }
  
      if(game.getTeams().size()&gt;0 &amp;&amp; 
         game.getTeams().get(0)!=null &amp;&amp; 
         game.getTeams().get(1)!=null &amp;&amp;
                (game.getTeams().get(0).getId() ==
                 game.getTeams().get(1).getId())) {
          model.addAttribute("message", "Teams cannot be the same. Try again");
          addAttributes(model);
          return (game.getId()==0) ? 
               "admin/games/newGameForm" : "admin/games/editGameForm";
      }
  
      if(game.getDate()!=null &amp;&amp; game.getDate().before(new Date())) {
          model.addAttribute("message", "Date must be in the future. Try again");
          addAttributes(model);
          return (game.getId()==0) ? 
                   "admin/games/newGameForm" : "admin/games/editGameForm";
      }
  
      redirectAttributes.addFlashAttribute("message", 
         (game.getId()==0) ? 
                   "New game was created succesfully" : "Game was updated succesfully");
      gameService.save(game);
      return "redirect:/admin/games/page/1/date/asc";
  }
  </code></pre>
  
  <p>To test this method, i wrote the following test: </p>
  
  <pre><code>@Test
  public void saveNewGame() throws Exception{
  
      mockMvc.perform(post("/admin/game/save")
              .param("date", "01/10/2018")
              .param("teams", "1"))
              .andExpect(status().is3xxRedirection())
              .andExpect(view().name("redirect:/admin/games/page/1/date/asc"))
              .andExpect(
                 MockMvcResultMatchers.
                 flash().
                 attribute("message", "New game was created succesfully"));
  
      ArgumentCaptor&lt;Game&gt; boundGame = ArgumentCaptor.forClass(Game.class);
      verify(gameService).save(boundGame.capture());
  
      Assert.assertEquals(DataLoader.parseDate("01/10/2018"), 
                          boundGame.getValue().getDate());
  }
  </code></pre>
  
  <p>Spring automatically creates this Game object and maps the values which i add to the MockMvcBuilder as parameters with the fields of the game object. This all works fine with primitive data types and the Data object i used above (the String value is perfectly transformed into a Date object because i annotated the Date attribute with a pattern, see the code below of the Game bean: </p>
  
  <pre><code>@Entity
  public class Game {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;
  
  @NotNull
  @ManyToMany (fetch = FetchType.EAGER)
  private List&lt;Team&gt; teams = new ArrayList&lt;&gt;();
  
  private Integer scoreTeamA;
  
  private Integer scoreTeamB;
  
  @ManyToOne
  private Location location;
  
  @ManyToOne
  private Competition competition;
  
  @NotNull
  @DateTimeFormat(pattern="dd/MM/yyyy")
  private Date date;
  
  public Game(){}
  
  //getters and setters and more
  }
  </code></pre>
  
  <p>But when a want to give a List of team objects, which is also attribute of the game objects, I get a BindingResultError which says: </p>
  
  <blockquote>
    <p>org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'game' on field 'teams': rejected value [1];
    codes
    [typeMismatch.game.teams,typeMismatch.teams,typeMismatch.java.util.List,typeMismatch];
    arguments
    [org.springframework.context.support.DefaultMessageSourceResolvable:
    codes [game.teams,teams]; arguments []; default message [teams]];
    default message [Failed to convert property value of type
    'java.lang.String' to required type 'java.util.List' for property
    'teams'; nested exception is java.lang.IllegalStateException: Cannot
    convert value of type 'java.lang.String' to required type
    'com.davidstranders.sportclub.model.Team' for property 'teams[0]': no
    matching editors or conversion strategy found]</p>
  </blockquote>
  
  <p>Above i tried to set a teamId as value for the teams object because this is also what happens when a user selects a team when he creates a game object, see the template (uses Thymeleaf):</p>
  
  <pre><code> &lt;div th:class="form-group" th:classappend="${#fields.hasErrors('teams')}? 'has-error'"&gt;
        &lt;label class="col-md-3 control-label"&gt;
              Teams &lt;span class="required"&gt;*&lt;/span&gt;
        &lt;/label&gt;
        &lt;div class="col-md-9"&gt;
             &lt;div class="col-md-5"&gt;
                  &lt;select class="form-control" th:field="*{teams}"&gt;
                      &lt;option value=""&gt;Select team&lt;/option&gt;
                      &lt;option th:each="team : ${teams}" 
                              th:value="${team.id}" 
                              th:text="${team.name}"&gt;teams
                      &lt;/option&gt;
                  &lt;/select&gt;
                  &lt;span th:if="${#fields.hasErrors('teams')}" 
                             th:errors="*{teams}"
                             th:class="help-block"&gt;Team Errors&lt;/span&gt;
             &lt;/div&gt;
             &lt;div class="col-md-1"&gt;&lt;label&gt;vs&lt;/label&gt;&lt;/div&gt;
             &lt;div class="col-md-5"&gt;
                  &lt;select class="form-control" th:field="*{teams}"&gt;
                      &lt;option value=""&gt;Select team&lt;/option&gt;
                      &lt;option th:each="team : ${teams}" 
                              th:value="${team.id}" 
                              th:text="${team.name}"&gt;teams
                      &lt;/option&gt;
                  &lt;/select&gt;
                  &lt;span th:if="${#fields.hasErrors('teams')}" 
                        th:errors="*{teams}" 
                        th:class="help-block"&gt;Team Errors&lt;/span&gt;
             &lt;/div&gt;
        &lt;/div&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p>Have anyone any idea how to deal with this? I tried to give a List with teams as String, or team instances as String, but this didn't work. </p>
  
  <p>Thanks for your help!!</p>