Controller 使用RestTemplate进行单元测试 spring mvc

来源:互联网 发布:京东盈利模式 知乎 编辑:程序博客网 时间:2024/06/05 02:10
看代码:
package test.com.wondersgroup.hs.test.controller;import org.junit.Test;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.client.RestTemplate;public class UserControllerTest {@Testpublic void testCreateUser(){RestTemplate restTemplate = new RestTemplate();MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();form.add("userName", "tom");form.add("password", "123456");form.add("age", "45");String result = restTemplate.postForObject("http://localhost:8080/SpringmvcDemo/users.html", form, String.class);System.out.println(result);}}

原创粉丝点击