I am trying to write Spring MVC Tests to test Spring controllers. Currently if I wanted to test a controller method with this signature:
@RequestMapping(value = "/new/save", method = RequestMethod.POST)
@Transactional
public String postGateway(@Valid GatewayForm gatewayForm, BindingResult bindingResult, RedirectAttributes flash, Model model)
I would use MockMVC in this manner to populate the GatewayForm object parameter:
mvc.perform(
post("/new/save")
.sessionAttr("account", account)
.param("serialNumber", "SN1QRTY334V")
.param("branchId", "1")
.param("model", "1")
.param("templateId", "1")
The problem with this is that I can only set String/primitive values onto the GatewayForm object using the .param(String, String) method.
So there are object instance variables on the GatewayForm above that I cannot set. I have seen other people instead post a JSON string of the object, but this would require changing the implementation of the controller methods to consume "application/json" which I do not want to do.
Can anybody advise?