spring boot使用TestRestTemplate集成测试 RESTful 接口
这篇文章没什么技术含量,只是单纯的记录一下如何用TestRestTemplate访问受security保护的api,供以后查阅。
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AccountControllerTests {
@Autowired
private TestRestTemplate restTemplate;
private HttpEntity httpEntity; /**
* 登录
* @throws Exception
*/
private void login() throws Exception {
String expectStr = "{\"code\":0,\"msg\":\"success\"}";
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", "183xxxxxxxx");
map.add("password", "123456");
ResponseEntity responseEntity = restTemplate.postForEntity("/api/account/sign_in", map, String.class);
//添加cookie以保持状态
HttpHeaders headers = new HttpHeaders();
String headerValue = responseEntity.getHeaders().get("Set-Cookie").toString().replace("[", "");
headerValue = headerValue.replace("]", "");
headers.set("Cookie", headerValue);
httpEntity = new HttpEntity(headers);
assertThat(responseEntity.getBody()).isEqualTo(expectStr);
} /**
* 登出
* @throws Exception
*/
private void logout() throws Exception {
String expectStr = "{\"code\":0,\"msg\":\"success\"}";
String result = restTemplate.postForObject("/api/account/sign_out", null, String.class, httpEntity);
httpEntity = null;
assertThat(result).isEqualTo(expectStr);
} /**
* 获取信息
* @throws Exception
*/
private void getUserInfo() throws Exception {
Detail detail = new Detail();
detail.setNickname("疯狂的米老鼠");
detail.setNicknamePinyin("fengkuangdemilaoshu");
detail.setSex(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
detail.setCreatedAt(sdf.parse("2017-11-03 16:43:27"));
detail.setUpdatedAt(sdf.parse("2017-11-03 16:43:27"));
Role role = new Role();
role.setName("ROLE_USER_NORMAL");
Set<Role> roles = new HashSet<>();
roles.add(role);
User user = new User();
user.setId(1L);
user.setPhone("183xxxxxxxx");
user.setEmail("xxxxxx@gmail.com");
user.setDetail(detail);
user.setRoles(roles);
ResultBean<User> resultBean = new ResultBean<>();
resultBean.setData(user);
ObjectMapper om = new ObjectMapper();
String expectStr = om.writeValueAsString(resultBean);
ResponseEntity<String> responseEntity = restTemplate.exchange("/api/user/get_user_info", HttpMethod.GET, httpEntity, String.class);
assertThat(responseEntity.getBody()).isEqualTo(expectStr);
} @Test
public void testAccount() throws Exception {
login();
getUserInfo();
logout();
}
spring boot使用TestRestTemplate集成测试 RESTful 接口的更多相关文章
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
- Spring Boot的数据访问:CrudRepository接口的使用
示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- Spring Boot的TestRestTemplate使用
文章目录 添加maven依赖 TestRestTemplate VS RestTemplate 使用Basic Auth Credentials 使用HttpClientOption 使用RestTe ...
- spring boot使用swagger生成api接口文档
前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...
- Spring Boot 2.x 编写 RESTful API (三) 程序层次 & 数据传输
用Spring Boot编写RESTful API 学习笔记 程序的层次结构 相邻层级的数据传输 JavaBean 有一个 public 的无参构造方法 属性 private,且可以通过 get.se ...
- Spring Boot 2.x 编写 RESTful API (二) 校验
用Spring Boot编写RESTful API 学习笔记 约束规则对子类依旧有效 groups 参数 每个约束用注解都有一个 groups 参数 可接收多个 class 类型 (必须是接口) 不声 ...
- 使用spring boot+mybatis+mysql 构建RESTful Service
开发目标 开发两个RESTful Service Method Url Description GET /article/findAll POST /article/insert 主要使用到的技术 j ...
- Spring Boot 集成 Swagger,生成接口文档就这么简单!
之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...
随机推荐
- squid,nginx,lighttpd反向代理的区别
反向代理从传输上分可以分为2种: 1:同步模式(apache-mod_proxy和squid) 2:异步模式(lighttpd 和 nginx) 在nginx的文档说明中,提到了异步传输模式并提到它可 ...
- 063 SparkStream数据接收方式
1.两种方式 2.Basic Source 由StreamingContext可以提供的API 上面做的wordcount中的方式就算是第一种方式. 3.Advanced Source 使用数据接收器 ...
- vim 初识
1. vim + 文件名.py :可以创建并打开vim命令模式 2. 命令模式中:shift + z + z :退出vim 3. 命令模式中(h : 左 , l : 右 , j : 下 , k : ...
- 在Visual Sutdio 2017中使用boost库
在Visual Sutdio 2017中使用boost库 转载 https://blog.csdn.net/u011054333/article/details/78648294 对C++有一 ...
- eclipse tomcat报Several ports(8005 8080 8009)端口被占用问题解决方案
在启动tomcat的时候eclipse突然报错 Several ports (8005,8080,8009) required by Tomcat v6.0 Server at localhost a ...
- 1613. 最高频率的IP
Description Given a string[] lines, each line will have an ip address , find the ip address with the ...
- H5与Native交互之JSBridge技术
一.原理篇 下面分别介绍IOS和Android与Javascript的底层交互原理 IOS 在讲解原理之前,首先来了解下iOS的UIWebView组件,先来看一下苹果官方的介绍: You can us ...
- Vue实现用户自定义上传头像裁剪
使用技术: vue.js2.0.cropperjs.canvas <template> <div id="app"> <div id=&q ...
- 翻转长方形 (不知名oj中一道个人私题)--单调栈维护最大子矩形
怎么分析这道题呢? 首先 ,我们注意到一点: 不管怎么操作,任意一个2*2方格中的 "#"个数的奇偶性是不变的. 所以,如果一个2*2方格中有奇数个"#",这个 ...
- C#常用字符串函数
Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 CompareOrdinal 与Compare一样,但不考虑文化背景 Format 格式化包含各种值的字符串和如何格式化每 ...