如何在功能/单元测试中传递查询/路径参数?

问题描述 投票:0回答:1

我想为我的控制器编写测试。我需要将参数传递给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);
java spring rest functional-testing
1个回答
1
投票

您必须在映射中包含param

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

© www.soinside.com 2019 - 2024. All rights reserved.