package com.yunen.camera.test;

 import org.junit.Before;
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.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import com.YunenCameraManagerApplication;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import lombok.extern.slf4j.Slf4j; /**
* 相机预置位测试类
*
* @version V1.0
* @author songxiaotong
* @date 2018年9月17日 下午2:08:14
* @Description
*/
@Slf4j
@Rollback(false)
// 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
// 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
@SpringBootTest(classes = YunenCameraManagerApplication.class)
public class CameraPreSetControllerTest {
/**
* WebApplicationContext
*/
@Autowired
private WebApplicationContext wac; private MockMvc mockMvc; /**
* 初始化mvc请求对象
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
} /**
* 新增
*
* @author songxiaotong
*/
@Test
public void create() {
try {
MockHttpSession session = new MockHttpSession(); JSONObject param = new JSONObject();
param.put("cameraId", "110");
param.put("name", "预置位名称");
param.put("zoom", 1222.1);
param.put("pan", 2222.2);
param.put("tilt", 3222.3); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/yunen/camera_pre_set/create");
requestBuilder.contentType(MediaType.APPLICATION_JSON); requestBuilder.content(JSONObject.toJSONString(param,
SerializerFeature.WriteMapNullValue));
// requestBuilder.content(param.toString()); requestBuilder.session(session); // 执行一个请求;
ResultActions result = mockMvc.perform(requestBuilder); // 添加执行完成后的断言
result.andExpect(MockMvcResultMatchers.status().isOk()); // 添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
result.andDo(MockMvcResultHandlers.print()); // 表示执行完成后返回相应的结果。
MvcResult mvcResult = result.andReturn(); log.info(mvcResult.getResponse().getContentAsString());
} catch (Exception e) {
log.error(e.getMessage());
}
} /**
* 更新
*
* @author songxiaotong
*/
@Test
public void update() {
String url = "/yunen/camera_pre_set/update";
JSONObject param = new JSONObject();
param.put("id", 1);
param.put("cameraId", "110");
param.put("name", "更新后的预置位名称");
param.put("zoom", 1222.1);
param.put("pan", 2222.2);
param.put("tilt", 3222.3);
// 调用测试模拟方法
requestMock(url, param);
} /**
* http请求模拟
*
* @param url 请求地址
* @param param 请求参数
* @author songxiaotong
*/
private void requestMock(String url, JSONObject param) {
try {
MockHttpSession session = new MockHttpSession(); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.post(url);
requestBuilder.contentType(MediaType.APPLICATION_JSON); requestBuilder.content(JSONObject.toJSONString(param,
SerializerFeature.WriteMapNullValue));
// requestBuilder.content(param.toString()); requestBuilder.session(session); // 执行一个请求;
ResultActions result = mockMvc.perform(requestBuilder); // 添加执行完成后的断言
result.andExpect(MockMvcResultMatchers.status().isOk()); // 添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
result.andDo(MockMvcResultHandlers.print()); // 表示执行完成后返回相应的结果。
MvcResult mvcResult = result.andReturn(); log.info(mvcResult.getResponse().getContentAsString());
} catch (Exception e) {
log.error(e.getMessage());
}
}
}

SpringBoot Junit Demo的更多相关文章

  1. activeMQ的spring、springboot的DEMO

    一.activeMQ实现spring的demo 1:pom.xml文件 <dependencies> <dependency> <groupId>junit< ...

  2. SpringBoot 入门 Demo

    SpringBoot   入门 Demo Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从 ...

  3. 基于Springboot+Junit+Mockito做单元测试

    前言 前面的两篇文章讨论过< 为什么要写单元测试,何时写,写多细 >和<单元测试规范>,这篇文章介绍如何使用Springboot+Junit+Mockito做单元测试,案例选取 ...

  4. SpringBoot:Demo

    目录 Demo 准备工作 登录+拦截器 Restful CRUD Restful架构 查询所有员工 添加员工 员工修改功能 HiddenHttpMethodFilter 删除员工 定制错误页面 注销功 ...

  5. SpringBoot + Swagger Demo

    Swagger是什么? Swagger 是一个规范且完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.  Swagger 的目标是对 REST API 定义一个标准且和语 ...

  6. 使用IDEA完成一个SpringBoot的demo

    打算开始做毕业设计了,写一些博客记录一下做毕业设计的过程. 前两天从老师那里拿了学长学姐做的非常简陋的代码,配置环境跑了一下,老师找我的时候说还剩下50%的工作,但感觉至少还有70%. 废话不多说,今 ...

  7. appium+java+junit demo运行

    对java熟悉一些,所以想用java把appium给做起来.今天用myeclipse给环境和Demo弄了一下,网上没有一篇全套资料的. 各块环境的搭建: 1.安装appium客户端,省略下载和安装步骤 ...

  8. SpringBoot入门Demo

    前言:相信做java后台编程的童鞋都知道Spring家族,Spring作为我们项目中必备的框架.JavaSpringBoot号称javaEE的颠覆者,这引起了本Y的好奇,这才花费了一点时间,学习了下s ...

  9. springboot+junit测试

    文章目录 一.junit断言 二.测试模块 三.使用Mockito作为桩模块 四.使用mockMvc测试web层 五.批量测试和测试覆盖率 参考视频:用Spring Boot编写RESTful API ...

随机推荐

  1. iTerm2 使用代理

    0x00 事件 因为 brew 安装极慢,所以需要 iTerm2 设置代理解决速度问题. 0x01 解决 代理软件开启本地 Http 端口: iTerm 设置代理: $ vim ~/.zshrc # ...

  2. RDIFramework.NET敏捷开发框架 ━ 工作流程组件介绍

    RDIFramework.NET,基于.NET的快速信息化系统敏捷开发.整合框架,给用户和开发者最佳的.Net框架部署方案. 1.RDIFramework.NET敏捷开发框架介绍 RDIFramewo ...

  3. 示例:Oracle表锁、行锁模拟和处理

    for update模拟锁表 --session 1 SQL> select * from tt for update; --session 2 SQL> update tt set id ...

  4. The 2017 ACM-ICPC Asia Shenyang Regional Contest

    传送门 F - Heron and His Triangle 直接打表找到规律\(f_i=4f_{i-1}+f_{i-2}\),然后大数预处理一下,对于询问直接输出就行. Code #include ...

  5. 201871010112-梁丽珍《面向对象程序设计(java)》第一周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接>    https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址> ...

  6. 《3D_Deep_Learning_for_Robot_Perception.pdf》

    https://github.com/PrincetonVision/marvin

  7. 设计模式-State(行为模式)-很好的实现了对象的状态逻辑与动作实现的分类,状态逻辑在State的派生类实现,动作可以放在Context类中实现。

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Context.h #pragma once class State; class Context { public: C ...

  8. 《细说PHP》第四版 样章 第18章 数据库抽象层PDO 11

    18.8.3  完美分页类的代码实现 分页类的编写除了需要使用在18.8.2节中提供的可以操作的3个成员方法,还需要更多的成员,但其他的成员方法和成员属性只需要内部使用,并不需要用户在对象外部操作,所 ...

  9. LeetCode | 机器人能否返回原点

    放假的时间已经过去一半了,每天坚持看一个多小时的书,时间虽然不多,但是能专心把书看进去就可以了.今天分享的是 LeetCode 上面的第 657 题,题目是<机器人能否返回原点>,这也是一 ...

  10. Node 之 Express 4x 骨架详解

    周末,没事就来公司加班继续研究一下Express ,这也许也是单身狗的生活吧. 1.目录结构: bin, 存放启动项目的脚本文件 node_modules, 项目所有依赖的库,以及存放 package ...