【发布时间】:2017-09-01 08:26:51
【问题描述】:
使用 Spring Boot,我想测试我的 @RestController,一切都很好,除非我尝试使用 @PathParam 测试请求映射。
这涉及到包含请求映射注解的接口(在本例中为 TestController)!如果我删除界面一切都很好......似乎是一个问题......
我的控制器:
public interface TestController {
@RequestMapping(value = "/bar/{foo}/baz", method = RequestMethod.GET)
String test(@PathVariable("foo") String foo);
@RestController
class TestControllerImpl implements TestController {
@Override
public String test(String foo) {
return foo;
}
}
}
我的测试:
@Test
public void notworkingtest() throws Exception {
//THIS TEST SHOULD WORK, BUT DON'T ...
final MockMvc mockMvc = ...
mockMvc.perform(get("/bar/{foo}/baz", "foovalue") // Or get("/bar/foovalue/baz")
.contentType("text/plain"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().string("foovalue"));
}
@Test
public void strangeworkingtest() throws Exception {
//THIS TEST WORKS, BUT WHY ?!?
final MockMvc mockMvc = ...
mockMvc.perform(get("/bar/{foo}/baz", "WhatEverValue")
.param("foo", "foovalue") // This param should be useless ...
.contentType("text/plain"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().string("foovalue"));
}
当我有 .param("foo","foovalue) 并保留 get("/bar/{foo}/baz", "WhatEverValue") ...
如果我删除控制器的接口,它就可以工作......
谁能给我解释一下?
谢谢
【问题讨论】:
-
您可以将 get("/test/{foo})" 替换为不带参数的 get("/test/foovalue")。
-
.param("foo", "foovalue")设置值 -
@sergiu 已经尝试 .. 做同样的行为 ...
-
@Sam :是的,但为什么我需要它,它应该由 uri 设置?
-
@Oziris 刚刚运行了您的测试,
notworkingtest确实通过了。
标签: java spring unit-testing spring-mvc spring-boot