本文介绍了弹簧mvc控制器的单元测试,整数值为@RequestParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器接受输入为 @RequestParam

I have the following controller which accept input as @RequestParam

@RequestMapping(value = "/fetchstatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Response fetchStatus(
        @RequestParam(value = "userId", required = true) Integer userId) {
    Response response = new Response();
    try {
        response.setResponse(service.fetchStatus(userId));
        response = (Response) Util.getResponse(
                response, ResponseCode.SUCCESS, FETCH_STATUS_SUCCESS,
                Message.SUCCESS);
    } catch (NullValueException e) {
        e.printStackTrace();
        response = (Response) Util.getResponse(
                response, ResponseCode.FAILED, e.getMessage(), Message.ERROR);
    } catch (Exception e) {
        e.printStackTrace();
        response = (Response) Util.getResponse(
                response, ResponseCode.FAILED, e.getMessage(), Message.ERROR);
    }
    return response;
}

我需要一个单元测试类,我是spring mvc的初学者。我不知道用 @RequestParam 编写测试类作为输入。

I need a unit test class for this and I am beginner with spring mvc. I don't know writing test classes with @RequestParam as input.

任何帮助都将不胜感激..

Any help will be appreciated ..

推荐答案

我刚刚解决了这个问题。我刚刚更改了网址。现在它在测试类中包含如下参数:

I just solved this issue. I just changed the url. Now it contains the parameter as below in test class:

mockMvc.perform(get("/fetchstatus?userId=1").andExpect(status().isOk());

这篇关于弹簧mvc控制器的单元测试,整数值为@RequestParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 17:39