如何对接受路径变量中的 id 列表的端点进行单元测试?
How to unit test an endpoint that accepts list of id's in a path variable?
问题
我有一个端点接受路径变量中的 ID 列表,但我无法在单元测试中传递输入。
我知道输入的形式是
Can anyone guide me what I am doing wrong? Thanks in advance.
下面是我的代码
@GetMapping("/{noteIds}")
public String getNotes(
@PathVariable List<String> noteIds) {
String methodName = "getNotes";
return "hello";
}
单位相同
mockMvc.perform(get("/{noteIds}", Arrays.asList("123","145"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(MockMvcResultHandlers.print());
预期输入
localhost:port/1,2,3,4(单元测试我是这样通过的)
预期输出
成功
实际输出
错误格式异常
您向端点传递了错误的输入。而不是通过 /{noteIds}
你应该通过 /123,145
例如:
mockMvc.perform(get("/123,145")
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(MockMvcResultHandlers.print());
问题
我有一个端点接受路径变量中的 ID 列表,但我无法在单元测试中传递输入。
我知道输入的形式是
Can anyone guide me what I am doing wrong? Thanks in advance.
下面是我的代码
@GetMapping("/{noteIds}")
public String getNotes(
@PathVariable List<String> noteIds) {
String methodName = "getNotes";
return "hello";
}
单位相同
mockMvc.perform(get("/{noteIds}", Arrays.asList("123","145"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(MockMvcResultHandlers.print());
预期输入
localhost:port/1,2,3,4(单元测试我是这样通过的)
预期输出
成功
实际输出
错误格式异常
您向端点传递了错误的输入。而不是通过 /{noteIds}
你应该通过 /123,145
例如:
mockMvc.perform(get("/123,145")
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(MockMvcResultHandlers.print());