我想为我的控制器编写一个测试。 而且我需要将参数传递给get()。 我该怎么办?
控制器:
@GetMapping("/getClientById")
public ModelAndView getClientById(Integer id){
return new ModelAndView("getClientById", "client", clientService.getClientById(id));
}
测试方法:
given().header("Content-Type", "application/x-www-form-urlencoded")
.when()
.get("getClientById/")//How can I put here an ID ?
.then()
.statusCode(200);
答案 0 :(得分:1)
您必须在映射中包含参数
@GetMapping("/getClientById/:clientId")
public ModelAndView getClientById(@PathParam("clientId") Integer id){
或
@GetMapping("/getClientById")
public ModelAndView getClientById(@QueryParam("id") Integer id){
然后分别
.get("getClientById/youridvalue")//How can I put here an ID ?
和
.get("getClientById?id=youridvalue")//How can I put here an ID ?
关于第二个选项,我认为有一种包含查询参数的方法,但我不知道您使用的是什么API,因此无法详细说明(可能还没有)