Spring MockMVC redirectUrlPattern 抛出 "No Ant-style path pattern"
Spring MockMVC redirectUrlPattern throws "No Ant-style path pattern"
我看过this question and this question。我想匹配 URL 模式,所以我根据这些问题的答案编写测试。
@Autowired
private MockMvc mockMvc;
// Ant pattern, example URL: /courses/1/edit
private static final String CREATE_SUCCESS_URL = "/courses/[0-9]+/edit";
// ...
this.mockMvc.perform(
post("/courses/create")
.with(csrf())
.param("name", "New Course"))
.andExpect(redirectedUrlPattern(CREATE_SUCCESS_URL));
我不确定正确的 Ant 模式语法,所以我也尝试了以下变体:
CREATE_SUCCESS_URL = "/courses/{[0-9]+}/edit"
CREATE_SUCCESS_URL = "/courses/\d+/edit"
CREATE_SUCCESS_URL = "/courses/\d/edit"
CREATE_SUCCESS_URL = "/courses/{\d+}/edit"
CREATE_SUCCESS_URL = "/courses/{id:\d+}/edit"
CREATE_SUCCESS_URL = "/courses/{id:[0-9]+}/edit"
但我总是遇到异常
java.lang.AssertionError: No Ant-style path pattern
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:36)
at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:66)
at org.springframework.test.web.servlet.result.MockMvcResultMatchers.lambda$redirectedUrlPattern(MockMvcResultMatchers.java:153)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:195)
at by.naxa.Whosebug.CoursesTest.givenNameWhenSubmitCreateCourseThenSuccess(
...
还有一件事。模式 CREATE_SUCCESS_URL = "/courses/?/edit"
工作正常,但我想确保我只匹配数字。
Spring 启动版本 2.1.2.RELEASE.
符合您要求的有效 Ant 路径模式如下所示:
CREATE_SUCCESS_URL = "/courses/{[0-9]*}/edit"
或
CREATE_SUCCESS_URL = "/courses/{\d*}/edit"
这个AssertionError
是org.springframework.util.AntPathMatcher
里面的以下方法造成的:
public boolean isPattern(String path) {
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
}
所以任何包含 *
或 ?
的字符串都可以使用。
我看过this question and this question。我想匹配 URL 模式,所以我根据这些问题的答案编写测试。
@Autowired
private MockMvc mockMvc;
// Ant pattern, example URL: /courses/1/edit
private static final String CREATE_SUCCESS_URL = "/courses/[0-9]+/edit";
// ...
this.mockMvc.perform(
post("/courses/create")
.with(csrf())
.param("name", "New Course"))
.andExpect(redirectedUrlPattern(CREATE_SUCCESS_URL));
我不确定正确的 Ant 模式语法,所以我也尝试了以下变体:
CREATE_SUCCESS_URL = "/courses/{[0-9]+}/edit"
CREATE_SUCCESS_URL = "/courses/\d+/edit"
CREATE_SUCCESS_URL = "/courses/\d/edit"
CREATE_SUCCESS_URL = "/courses/{\d+}/edit"
CREATE_SUCCESS_URL = "/courses/{id:\d+}/edit"
CREATE_SUCCESS_URL = "/courses/{id:[0-9]+}/edit"
但我总是遇到异常
java.lang.AssertionError: No Ant-style path pattern
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:36)
at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:66)
at org.springframework.test.web.servlet.result.MockMvcResultMatchers.lambda$redirectedUrlPattern(MockMvcResultMatchers.java:153)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:195)
at by.naxa.Whosebug.CoursesTest.givenNameWhenSubmitCreateCourseThenSuccess(
...
还有一件事。模式 CREATE_SUCCESS_URL = "/courses/?/edit"
工作正常,但我想确保我只匹配数字。
Spring 启动版本 2.1.2.RELEASE.
符合您要求的有效 Ant 路径模式如下所示:
CREATE_SUCCESS_URL = "/courses/{[0-9]*}/edit"
或
CREATE_SUCCESS_URL = "/courses/{\d*}/edit"
这个AssertionError
是org.springframework.util.AntPathMatcher
里面的以下方法造成的:
public boolean isPattern(String path) {
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
}
所以任何包含 *
或 ?
的字符串都可以使用。