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

时间:2018-09-12 19:42:04

标签: java spring rest functional-testing

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

1 个答案:

答案 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,因此无法详细说明(可能还没有)