I\'m writing tests to verify that I can do a generic form post to our API.
I\'ve also added quite some debugging, but I noticed that the data posted by an actual for
You could also use this small library I created: https://github.com/f-lopes/spring-mvc-test-utils/.
Add dependency in pom.xml:
io.florianlopes
spring-mvc-test-utils
1.0.1
test
Use it with MockMvc:
mockMvc.perform(MockMvcRequestBuilderUtils.postForm("/users", new AddUserForm("John", "Doe", null, new Address(1, "Street", 5222, "New York"))))
.andExpect(MockMvcResultMatchers.status().isFound())
.andExpect(MockMvcResultMatchers.redirectedUrl("/users"))
.andExpect(MockMvcResultMatchers.flash().attribute("message", "success"));
This library simply adds the parameters to the MockMvc request, according to the form object.
Here is a detailed tutorial I wrote: https://blog.florianlopes.io/tool-for-spring-mockmvcrequestbuilder-forms-tests/