Controller 使用RestTemplate進行單元測試 spring mvc

看代碼:
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 {
	@Test
	public 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);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章