title: Mockito MVC - how to pass value for @RequestParam binding a string array
body: <p>I'd like to write a unit test using Mockito.</p>
  
  <p>The method to be tested is written by <strong>Spring MVC</strong> as follows:</p>
  
  <pre><code>@RequestMapping(value = "/save", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
  @ResponseBody
  public String save(String appName,
          @RequestParam(value = "platforms[]") String[] platforms,
          HttpServletRequest request) {}
  </code></pre>
  
  <p>I tried several ways to mock request value <strong>platforms</strong> which is <strong>binding a string array</strong> like '<strong><em>string[]</em></strong>'. </p>
  
  <p>But, it seems it is still not working: the platforms value is null.</p>
  
  <p>Here are the ways I tried:</p>
  
  <p><strong>Way#1</strong></p>
  
  <pre><code>final String[] platforms = { "Android", "iOS" };
  request.setAttribute("platforms", platforms);
  request.setAttribute("platforms[]", platforms);
  </code></pre>
  
  <p><strong>Way#2</strong></p>
  
  <pre><code>mockMvc.perform(MockMvcRequestBuilders.post("/application/save")
              .session(session).requestAttr("platforms[]", platforms)
              .requestAttr("platforms", platforms)
              .param("appName", "Just for test"));
  </code></pre>
  
  <p>Note: request is a mock object (<strong>MockHttpServletRequest</strong>)</p>
  
  <p>Hope you can give me some idea to solve this problem. Thanks.</p>
  
  <p>For more information, here are the print result when doing way#2:</p>
  
  <pre><code>MockHttpServletRequest:
       HTTP Method = POST
       Request URI = /application/save
        Parameters = {appName=[Just for test]}
           Headers = {}
  
           Handler:
              Type = com.xxx.controller.ApplicationController
            Method = public java.lang.String com.xxx.controller.ApplicationController.save(java.lang.String,java.lang.String[],javax.servlet.http.HttpServletRequest)
  
  Resolved Exception:
              Type = org.springframework.web.bind.MissingServletRequestParameterException
  
      ModelAndView:
         View name = null
              View = null
             Model = null
  
          FlashMap:
  
   MockHttpServletResponse:
            Status = 400
     Error message = Required String[] parameter 'platforms[]' is not present
           Headers = {}
      Content type = null
              Body = 
     Forwarded URL = null
    Redirected URL = null
           Cookies = []
  </code></pre>