mercredi 29 juin 2016

Mockito MVC - how to pass value for @RequestParam binding a string array

I'd like to write a unit test using Mockito.

The method to be tested is written by Spring MVC as follows:

@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) {}

I tried several ways to mock request value platforms which is binding a string array like 'string[]'.

But, it seems it is still not working: the platforms value is null.

Here are the ways I tried:

Way#1

final String[] platforms = { "Android", "iOS" };
request.setAttribute("platforms", platforms);
request.setAttribute("platforms[]", platforms);

Way#2

mockMvc.perform(MockMvcRequestBuilders.post("/application/save")
            .session(session).requestAttr("platforms[]", platforms)
            .requestAttr("platforms", platforms)
            .param("appName", "Just for test"));

Note: request is a mock object (MockHttpServletRequest)

Hope you can give me some idea to solve this problem. Thanks.

Aucun commentaire:

Enregistrer un commentaire