这篇文章没什么技术含量,只是单纯的记录一下如何用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 接口的更多相关文章

  1. Spring Boot 集成 Swagger 生成 RESTful API 文档

    原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...

  2. Spring Boot的数据访问:CrudRepository接口的使用

    示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...

  3. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  4. Spring Boot的TestRestTemplate使用

    文章目录 添加maven依赖 TestRestTemplate VS RestTemplate 使用Basic Auth Credentials 使用HttpClientOption 使用RestTe ...

  5. spring boot使用swagger生成api接口文档

    前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...

  6. Spring Boot 2.x 编写 RESTful API (三) 程序层次 & 数据传输

    用Spring Boot编写RESTful API 学习笔记 程序的层次结构 相邻层级的数据传输 JavaBean 有一个 public 的无参构造方法 属性 private,且可以通过 get.se ...

  7. Spring Boot 2.x 编写 RESTful API (二) 校验

    用Spring Boot编写RESTful API 学习笔记 约束规则对子类依旧有效 groups 参数 每个约束用注解都有一个 groups 参数 可接收多个 class 类型 (必须是接口) 不声 ...

  8. 使用spring boot+mybatis+mysql 构建RESTful Service

    开发目标 开发两个RESTful Service Method Url Description GET /article/findAll POST /article/insert 主要使用到的技术 j ...

  9. Spring Boot 集成 Swagger,生成接口文档就这么简单!

    之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...

随机推荐

  1. 数据库构架设计中的Shared Everthting、Shared Nothing、和Shared Disk

    Shared Everthting:一般是针对单个主机,完全透明共享CPU/MEMORY/IO,并行处理能力是最差的,典型的代表SQLServer Shared Disk:各个处理单元使用自己的私有 ...

  2. IDEA学习——模板及其常用模板

    模板及其常用模板 (1)psvm (2)sout sout / soutp / soutv / 变量.sout (3)fori iter增强for循环 itar普通for循环 (4)list.for ...

  3. 054 kafka内部机制

    一:数据格式与数据存储 1.总结 存储在磁盘文件中(index+log) 顺序读写的 基于offset偏移量来管理数据的(主要是读操作) 由分区器根据key值决定数据分布到哪个分区,默认使用hash ...

  4. IDEA创建SpringBoot项目

    创建SpringBoot有三种方式: 方式一:(常用方式)

  5. TF:Tensorflor之session会话的使用,定义两个矩阵,两种方法输出2个矩阵相乘的结果—Jason niu

    import tensorflow as tf matrix1 = tf.constant([[3, 20]]) matrix2 = tf.constant([[6], [100]]) product ...

  6. Dev-C++安装第三方库boost

    Dev-C++安装第三方库boost  转 https://www.jianshu.com/p/111571e4d6f5?utm_source=oschina-app 之前鉴于codeblocks界面 ...

  7. To the Max POJ - 1050 (最大子段和)

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  8. CSS3 animation 练习

    css3 的动画让 html 页面变得生机勃勃,但是如何用好动画是一门艺术,接下来我来以一个demo为例,来练习css3 animation. 我们先详细了解一下animation 这个属性. ani ...

  9. PAT (Advanced Level) Practise 1001 解题报告

    GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...

  10. XamarinSQLite教程在Xamarin.Android项目中使用数据库

    XamarinSQLite教程在Xamarin.Android项目中使用数据库 在Xamarin.Android项目中使用预设数据库的具体操作步骤如下: (1)创建一个Xamarin.Android项 ...