简体   繁体   English

如何测试使用HttpServletRequest的休息服务?

[英]How to test a rest service that uses a HttpServletRequest?

I'm trying to write a test for a rest service: 我正在尝试为休息服务编写测试:

I have this post request: 我有这个帖子请求:

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(HttpServletRequest request) {
    String username = request.getParameter("username");
    ...
}

And I'm trying to test it using RestTemplate, but I couldn't find any example where I can set the username in the HttpServletRequest. 我正在尝试使用RestTemplate测试它,但我找不到任何可以在HttpServletRequest中设置用户名的示例。

Is there a solution at this problem? 这个问题有解决方案吗?

public String postToTest(String username) {
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("username", username);
    return restTemplate.postForObject("http://localhost:8080/soa-server/user/", map, String.class);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM