Is there a way to pass an entire form object on mock request when integration testing a spring mvc web app? All I can find is to pass each field separately as a param like this:
mockMvc.perform(post("/somehwere/new").param("items[0].value","value"));
Which is fine for small forms. But what if my posted object gets larger? Also it makes the test code look nicer if I can just post an entire object.
Specifically I'd like to test the selection of multiple items by checkbox and then posting them. Of course I could just test posting a single item, but I was wondering..
We're using spring 3.2.2 with the spring-test-mvc included.
My Model for the form looks something like this:
NewObject {
List<Item> selection;
}
I've tried calls like this:
mockMvc.perform(post("/somehwere/new").requestAttr("newObject", newObject)
to a Controller like this:
@Controller
@RequestMapping(value = "/somewhere/new")
public class SomewhereController {
@RequestMapping(method = RequestMethod.POST)
public String post(
@ModelAttribute("newObject") NewObject newObject) {
// ...
}
But the object will be empty (yes I've filled it before in the test)
The only working solution I found was using @SessionAttribute like this: Integration Testing of Spring MVC Applications: Forms
But I dislike the idea of having to remember to call complete at the end of every controller where I need this. After all the form data does not have to be inside the session, I only need it for the one request.
So the only thing I can think of right now is to write some Util class that uses the MockHttpServletRequestBuilder to append all the object fields as .param using reflections or individually for each test case..
I don't know, feeld un-intuitive..
Any thoughts / ideas on how I might make my like easier? (Apart from just calling the controller directly)
Thanks!
MediaType.APPLICATION_FORM_URLENCODED
data so my test should send that data.. I've even tried the convert from the link I postet sending byte[] to the controller but it still won't pick it up.. – Bodycheck