栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Spring MockMVC测试可选路径变量

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Spring MockMVC测试可选路径变量

使用这样的

@RequestMapping
值数组…

@RequestMapping(value = {"/some/uri/{foo}", "/some/uri/{foo}/{bar}"}, method = RequestMethod.PUT)public ResponseEntity<String> someMethod(@PathVariable("foo") String foo, @PathVariable(value = "bar", required = false) String bar) {    return new ResponseEntity<>(foo + " and " + (bar == null ? "<null>" : bar), HttpStatus.OK);}

…将使该测试通过:

@Testpublic void someMethodTest() throws Exception {    MvcResult mvcResult = mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", "bar")) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and bar", mvcResult.getResponse().getContentAsString());    mvcResult = mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", null)) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());    mvcResult = mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", "")) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());    mvcResult = mockMvc.perform(put("/some/uri/{foo}", "foo")) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());}

当然,这似乎是最简单的解决方案,并且它对Swagger之类的工具可能更友好,因为它使映射变得明确。

但是,您也可以声明一个通配符映射,然后在控制器方法中使用路径匹配器来解释请求URI。例如,此方法…

private final AntPathMatcher antPathMatcher = new AntPathMatcher();@RequestMapping(value = "/some/uri/with/wildcards/**", method = RequestMethod.PUT)public ResponseEntity<String> someMethod(HttpServletRequest request) {    String matched = antPathMatcher.extractPathWithinPattern( (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE), request.getPathInfo());    // ugly parsing pre to read the path variables, allowing for the optionality of the second one    String foo = matched;    String bar = null;    String[] pathVariables = matched.split("/");    if (pathVariables.length > 1) {        foo = pathVariables[0];        bar = pathVariables[1];    }    return new ResponseEntity<>(foo + " and " + (bar == null ? "<null>" : bar), HttpStatus.OK);}

…将使该测试通过:

@Testpublic void someMethodTestWithWildcards() throws Exception {    MvcResult mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}/{bar}", "foo", "bar")) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and bar", mvcResult.getResponse().getContentAsString());    mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}/{bar}", "foo", null)) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());    mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}/{bar}", "foo", "")) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());    mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}", "foo")) .andExpect(status().isOk()).andReturn();    Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/408567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号