RestTemplate
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
FormUserControllerTests
import com.baizhi.SpringBootApplication;
import com.baizhi.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
@SpringBootTest(classes = {SpringBootApplication.class})
@RunWith(SpringRunner.class)
public class FormUserControllerTest {
@Autowired
private RestTemplate restTemplate;
private String prefix="http://localhost:8989/formUserManager";
/*
@PostMapping(value = "/registerUser")
public User registerUser(User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile)
*/
@Test
public void testRegisterUser(){
String url=prefix+"/registerUser";
//构建表单参数
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
formData.add("name","王五");
formData.add("password","123456");
formData.add("sex","true");
formData.add("birthDay","2018-01-26");
formData.add("photo","user1.png");
formData.add("email","1152926811@qq.com");
//上传文件信息
FileSystemResource fileSystemResource=new FileSystemResource("C:\\Users\\Administrator\\Desktop\\头像\\哈哈哈.jpg");
formData.add("multipartFile",fileSystemResource);
User dbUser = restTemplate.postForObject(url, formData, User.class);
assertNotNull("用户:",dbUser);
System.out.println(dbUser);
}
/*
@PostMapping(value = "/userLogin")
public User userLogin(User user)
*/
@Test
public void testUserLogin(){
String url=prefix+"/userLogin";
//构建表单参数
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
formData.add("name","王小五");
formData.add("password","123456");
User dbUser = restTemplate.postForObject(url, formData, User.class);
assertNotNull("用户:",dbUser);
System.out.println(dbUser);
}
/*
@PostMapping(value = "/addUser")
public User addUser(User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException
*/
@Test
public void testAddUser(){
String url=prefix+"/addUser";
//构建表单参数
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
formData.add("name","赵六");
formData.add("password","123456");
formData.add("sex","true");
formData.add("birthDay","2018-01-26");
formData.add("photo","user.png");
formData.add("email","1152926811@qq.com");
//上传文件信息
FileSystemResource fileSystemResource=new FileSystemResource("C:\\Users\\Administrator\\Desktop\\头像\\哈哈哈.jpg");
formData.add("multipartFile",fileSystemResource);
User dbUser = restTemplate.postForObject(url, formData, User.class);
assertNotNull("用户:",dbUser);
System.out.println(dbUser);
}
/*
@PutMapping(value = "/updateUser")
public void updateUser(User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException
*/
@Test
public void testUpdateUser(){
String url=prefix+"/updateUser";
//构建表单参数
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
formData.add("id",20);
formData.add("name","haha");
formData.add("password","123456");
formData.add("sex","false");
formData.add("birthDay","2018-01-26");
formData.add("photo","user2.png");
formData.add("email","1152926811@qq.com");
//上传文件信息
FileSystemResource fileSystemResource=new FileSystemResource("C:\\Users\\Administrator\\Desktop\\头像\\哈哈哈.jpg");
formData.add("multipartFile",fileSystemResource);
restTemplate.put(url, formData);
}
/*
@DeleteMapping(value = "/deleteUserByIds")
public void delteUserByIds(@RequestParam(value = "ids") Integer[] ids)
*/
@Test
public void testDeleteUserByIds(){
String url=prefix+"/deleteUserByIds?ids={id}";
//构建表单参数
Map<String,Object> params=new HashMap<String,Object>();
params.put("id","14,15,16");
restTemplate.delete(url,params);
}
/*
@GetMapping(value = "/queryUserByPage")
public List<User> queryUserByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNow,
@RequestParam(value = "rows",defaultValue = "10") Integer pageSize,
@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value)
*/
@Test
public void testQueryUserByPage(){
String url=prefix+"/queryUserByPage?page={p}&rows={r}&column={c}&value={v}";
//构建表单参数
Map<String,Object> params=new HashMap<String,Object>();
params.put("p",1);
params.put("r",10);
params.put("c","name");
params.put("v","小");
User[] users = restTemplate.getForObject(url, User[].class, params);
for (User user : users) {
System.out.println(user);
}
}
/*
@GetMapping(value = "/queryUserCount")
public Integer queryUserCount( @RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value)
*/
@Test
public void testQueryUserCount(){
String url=prefix+"/queryUserCount?column={c}&value={v}";
//构建表单参数
Map<String,Object> params=new HashMap<String,Object>();
params.put("c","name");
params.put("v","小");
Integer count = restTemplate.getForObject(url, Integer.class, params);
System.out.println("Count:"+count);
}
/*
@GetMapping(value = "/queryUserById")
public User queryUserById(@RequestParam(value = "id") Integer id)
*/
@Test
public void testQueryUserById(){
String url=prefix+"/queryUserById?id={id}";
//构建表单参数
Map<String,Object> params=new HashMap<String,Object>();
params.put("id","20");
User user = restTemplate.getForObject(url, User.class, params);
System.out.println(user);
}
}
RestUserControllerTests(json)
import com.baizhi.SpringBootApplication;
import com.baizhi.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
@SpringBootTest(classes = {SpringBootApplication.class})
@RunWith(SpringRunner.class)
public class RestUserControllerTest {
@Autowired
private RestTemplate restTemplate;
private String urlPrefix="http://localhost:8989/restUserManager";
/*
@PostMapping(value = "/registerUser")
public User registerUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException {
*/
@Test
public void testRegisterUser(){
String url=urlPrefix+"/registerUser";
//模拟表单数据
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
User user =new User("张晓磊",true,"123456",new Date(),"aa.png","1152926811@qq.com");
formData.add("user",user);
//模拟文件上传
FileSystemResource fileSystemResource=new FileSystemResource("C:\\Users\\Administrator\\Desktop\\头像\\哈哈哈.jpg");
formData.add("multipartFile",fileSystemResource);
User registerUser = restTemplate.postForObject(url, formData, User.class);
assertNotEquals("用户ID",registerUser.getId());
}
/*
@PostMapping(value = "/userLogin")
public User userLogin(@RequestBody User user)
*/
@Test
public void testUserLogin(){
String url=urlPrefix+"/userLogin";
//模拟表单数据
User user=new User();
user.setName("张晓磊");
user.setPassword("123456");
User loginUser = restTemplate.postForObject(url, user, User.class);
assertNotEquals("用户ID",loginUser.getId());
}
/*
@PostMapping(value = "/addUser")
public User addUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException
*/
@Test
public void testAddUser(){
String url=urlPrefix+"/addUser";
//模拟表单数据
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
User user =new User("温晓琪",true,"123456",new Date(),"aa.png","1152926811@qq.com");
formData.add("user",user);
//模拟文件上传
FileSystemResource fileSystemResource=new FileSystemResource("/Users/admin/Desktop/head.png");
formData.add("multipartFile",fileSystemResource);
User dbUser = restTemplate.postForObject(url, formData, User.class);
assertNotNull("用户ID",dbUser.getId());
}
/*
@PutMapping(value = "/updateUser")
public void updateUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException {
*/
@Test
public void testUpdateUser(){
String url=urlPrefix+"/updateUser";
//模拟表单数据
MultiValueMap<String,Object> formData=new LinkedMultiValueMap<String,Object>();
User user =new User("温晓琪",false,"123456",new Date(),"aa.png","1152926811@qq.com");
user.setId(27);
formData.add("user",user);
//模拟文件上传
FileSystemResource fileSystemResource=new FileSystemResource("/Users/admin/Desktop/head.png");
formData.add("multipartFile",fileSystemResource);
restTemplate.put(url, formData);
}
/*
@DeleteMapping(value = "/deleteUserByIds")
public void delteUserByIds(@RequestParam(value = "ids") Integer[] ids)
*/
@Test
public void testDeleteUser(){
String url=urlPrefix+"/deleteUserByIds?ids={id}";
Map<String,Object> parameters=new HashMap<String,Object>();
parameters.put("id","4,5,6");
restTemplate.delete(url,parameters);
}
/*
@GetMapping(value = "/queryUserByPage")
public List<User> queryUserByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNow,
@RequestParam(value = "rows",defaultValue = "10") Integer pageSize,
@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value)
*/
@Test
public void testQueryUserByPage(){
String url=urlPrefix+"/queryUserByPage?page={page}&rows={rows}&column={column}&value={value}";
Map<String, Object> params= new HashMap<String, Object>();
params.put("page",1);
params.put("rows",10);
params.put("column","name");
params.put("value","小");
User[] users=restTemplate.getForObject(url,User[].class,params);
assertNotNull("用户数据",users);
}
/*
@GetMapping(value = "/queryUserCount")
public Integer queryUserCount(@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value)
*/
@Test
public void testQueryUserCount(){
String url=urlPrefix+"/queryUserCount?column={column}&value={value}";
//模拟表单数据
Map<String,Object> parameters=new HashMap<>();
parameters.put("column","name");
parameters.put("value","晓");
Integer count = restTemplate.getForObject(url, Integer.class,parameters);
assertNotNull(count);
}
/*
@GetMapping(value = "/queryUserById")
public User queryUserById(@RequestParam(value = "id") Integer id)
*/
@Test
public void testQueryUserById(){
String url=urlPrefix+"/queryUserById?id={id}";
//模拟表单数据
Map<String,Object> parameters=new HashMap<>();
parameters.put("id","2");
User user = restTemplate.getForObject(url, User.class,parameters);
assertNotNull("用户ID",user.getId());
}
}