Spring MockMvc redirectedUrl with pattern

前端 未结 2 1123
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 14:19

I have a simple PersonController class that provides save() method to persist the object from http post request.

package org.rw.control         


        
相关标签:
2条回答
  • 2021-02-08 14:31

    Since spring 4.0 you can use redirectedUrlPattern as pointed by Paulius Matulionis

    As of spring 3.x this is not supported out of the box but you can easily add you custom result matcher

    private static ResultMatcher redirectedUrlPattern(final String expectedUrlPattern) {
        return new ResultMatcher() {
            public void match(MvcResult result) {
                Pattern pattern = Pattern.compile("\\A" + expectedUrlPattern + "\\z");
                assertTrue(pattern.matcher(result.getResponse().getRedirectedUrl()).find());
            }
        };
    }
    

    And use it like build-in matcher

     mockMvc.perform(
        post("/person/save", new Object[0])
            .param("firstName", "JunitFN")
            .param("lastName", "JunitLN")
            .param("gender", "M")
            .param("dob", "11/02/1989")
     ).andExpect(
            redirectedUrlPattern("view/[0-9]+")
     );
    
    0 讨论(0)
  • 2021-02-08 14:41

    Since 4.0 it is available in Spring itself.

    Please check here.

    0 讨论(0)
提交回复
热议问题