如何在 functional/unit 测试中传递 query/path 参数?

How to pass a query/path param in functional/unit test?

我想为我的控制器编写一个测试。 我需要将一个参数传递给 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);

您必须在映射中包含参数

@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,所以无法详细说明(可能还没有)