Testing Form posts through MockMVC

前端 未结 3 901
悲&欢浪女
悲&欢浪女 2020-12-30 21:11

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

3条回答
  •  离开以前
    2020-12-30 21:59

    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/

提交回复
热议问题