springboot之单元测试
springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例
工程层次结构如图

代码如下:
controller:
package com.rookie.bigdata.controller; import com.rookie.bigdata.domain.User;
import com.rookie.bigdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* controller层
* Created by on 2018/9/28.
*/
@RestController
public class UserController { @Autowired
private UserService userService; /**
* 查询用户
*
* @return
*/
@GetMapping(value = "/user")
public User findUser() {
return userService.findOne(10);
} }
User:
package com.rookie.bigdata.domain; /**
* domain实体对象
* Created by on 2018/9/28.
*/
public class User {
private int id;
private String name;
private Integer age; public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public User() {
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
service:
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.User;
import org.springframework.stereotype.Service; /**
* service层
* Created by on 2018/9/28.
*/
@Service
public class UserService { public User findOne(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三");
user.setAge(23);
return user;
}
}
启动程序:
package com.rookie.bigdata; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* 应用程序启动类
* Created by on 2018/8/2.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); }
}
测试类:
package com.rookie.bigdata.controller; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.junit.Assert.*; /**
* 测试contoller层
* Created by on 2018/9/28.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest { @Autowired
private MockMvc mvc; @Test
public void findUser() throws Exception {
// mvc.perform(MockMvcRequestBuilders.get("/user"))
// .andExpect(MockMvcResultMatchers.status().isOk()); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/user"))
.andExpect(MockMvcResultMatchers.status().isOk())//模拟发送get请求
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))//预期返回值的媒体类型 application/json;charset=UTF-8
.andReturn();//返回执行的请求结果 System.out.println(mvcResult.getResponse().getContentAsString()); } }
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.User;
import org.junit.Assert;
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.test.context.junit4.SpringRunner; /**
* 测试service层
* Created by on 2018/9/28.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest { @Autowired
private UserService userService; @Test
public void findOne() throws Exception { User user = userService.findOne(1); Assert.assertEquals(new Integer(23),user.getAge());
} }
springboot之单元测试的更多相关文章
- SpringBoot系列: 单元测试2
之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...
- SpringBoot系列: 单元测试
SpringBoot 项目单元测试也很方便, Web项目中单元测试应该覆盖:1. Service 层2. Controller 层 本文前半部分讲解是一些测试基础配置. 对于Service和Contr ...
- SpringBoot项目单元测试
关于SpringBoot的单元测试,描述一下三种单元测试的方式. 1.约定 单元测试代码写在src/test/java目录下单元测试类命名为*Test,前缀为要测试的类名 2. 使用mock方式单元测 ...
- 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解
========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...
- SpringBoot进行单元测试
SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...
- springboot的单元测试(总结两种)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- springBoot(5)---单元测试,全局异常
单元测试,全局异常 一.单元测试 1.基础版 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> <dependency> <g ...
- ABAP和Java SpringBoot的单元测试
ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...
- Springboot整合单元测试
概述 对于简单易懂的小项目而言,可以不适用单元测试对平时开发没有什么影响,但是对于大型项目,单纯的依赖 “手点功能测试”, 那简直就是灾难,好了,说道这里,应该明白测试的一个重要性了,,,接下来,我们 ...
随机推荐
- 使用Sybmol模块来构建神经网络
符号编程 在之前的文章,我们介绍了NDArray模块,它是MXNet中处理数据的核心模块,我们可以使用NDArray完成非常丰富的数学运算.实际上,我们完全可以使用NDArray来定义神经网络,这种方 ...
- python爬虫学习之爬取全国各省市县级城市邮政编码
实例需求:运用python语言在http://www.ip138.com/post/网站爬取全国各个省市县级城市的邮政编码,并且保存在excel文件中 实例环境:python3.7 requests库 ...
- pwnable.kr详细通关秘籍(二)
i春秋作家:W1ngs 原文来自:pwnable.kr详细通关秘籍(二) 0x00 input 首先看一下代码: 可以看到程序总共有五步,全部都满足了才可以得到flag,那我们就一步一步来看 这道题考 ...
- JSON库的使用研究(三)
怎么选择JSON库? 从整体测试结果来看,总结如下: 用于序列化.反序列的功能,数量量小,吞吐量不大于10000每秒的,选择gson: 用于解析JSON的,还是用Fastjson吧,虽然听说坑很多. ...
- Docker学习之2——镜像
镜像(Images) 镜像是Docker的三大核心之一,类似于虚拟机,作用和虚拟机是一样的,唯独是组成部分会有些区别.简单的说如果我们想启动一个容器就必须要有镜像.docker运行容器前需要本地存在对 ...
- C++11实现一个轻量级的AOP框架
AOP介绍 AOP(Aspect-Oriented Programming,面向方面编程),可以解决面向对象编程中的一些问题,是OOP的一种有益补充.面向对象编程中的继承是一种从上而下的关系,不适合定 ...
- Microsoft 机器学习产品体系对比和介绍
Microsoft 提供多种多样的产品选项用于生成.部署和管理机器学习模型. 本文将比较这些产品,并帮助你选择所需的产品,以便最有效地开发机器学习解决方案. 机器学习产品 描述 作用 Azure云端服 ...
- 旅行,说走就走 Help? [C++数据类型和表达式]
摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 乐天派.我却喜欢和老妈说"老妈小时候喜欢羡慕别人有动力,现在看 ...
- linux 命令 — find
find 基本形式 find base_path base_path可以是任何目录,find会从该目录开始往下寻找 find . -print 列出当前目录下所有的文件和目录,以'\n'作为分隔符 f ...
- How Tomcat works — 二、tomcat启动(1)
主要介绍tomcat启动涉及到的一些接口和类. 目录 概述 tomcat包含的组件 server和service Lifecycle Container Connector 总结 概述 tomcat作 ...