springboot测试
一、单元测试
在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了。
1、在pom包中添加spring-boot-starter-test包引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、开发测试类
以最简单的helloworld为例,在测试类的类头部需要添加:@RunWith(SpringRunner.class)
和@SpringBootTest
注解,在测试方法的顶端添加@Test
即可,最后在方法上点击右键run就可以运行。
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest { @Autowired
private MailService mailService; @Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("1249736987@qq.com", "test simple mail", " zmc");
} @Test
public void hello() {
System.out.println("hello world,zmc");
} }
实际使用中,可以按照项目的正常使用去注入dao层代码或者是service层代码进行测试验证,spring-boot-starter-test提供很多基础用法,更难得的是增加了对Controller层测试的支持。
//简单验证结果集是否正确
Assert.assertEquals(3, userMapper.getAll().size()); //验证结果集,提示
Assert.assertTrue("错误,正确的返回值为200", status == 200);
Assert.assertFalse("错误,正确的返回值为200", status != 200);
引入了MockMvc
支持了对Controller层的测试,简单示例如下:
public class HelloControlerTests { private MockMvc mvc; //初始化执行
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
} //验证controller是否正常响应并打印返回结果
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
} //验证controller是否正常响应并判断返回结果是否正确
@Test
public void testHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
} }
单元测试是验证你代码第一道屏障,要养成每写一部分代码就进行单元测试的习惯,不要等到全部集成后再进行测试,集成后因为更关注整体运行效果,很容易遗漏掉代码底层的bug.
二、集成测试
整体开发完成之后进入集成测试,spring boot项目的启动入口在 Application类中,直接运行run方法就可以启动项目,但是在调试的过程中我们肯定需要不断的去调试代码,如果每修改一次代码就需要手动重启一次服务就很麻烦,spring boot非常贴心的给出了热部署的支持,很方便在web项目中调试使用。
pom需要添加以下的配置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
添加以上配置后,项目就支持了热部署,非常方便集成测试。
springboot测试的更多相关文章
- springboot测试、打包、部署
本文使用<springboot集成mybatis(一)>项目,依次介绍springboot测试.打包.部署. 大多数朋友是做后端的,也就是为其他系统或者前端UI提供Rest API服务. ...
- SpringBootTest单元测试实战、SpringBoot测试进阶高级篇之MockMvc讲解
1.@SpringBootTest单元测试实战 简介:讲解SpringBoot的单元测试 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> &l ...
- Redis3.2.5 集群搭建以及Spring-boot测试
1:集群中的机器信息 IP PORT 192.168.3.10 7000,7001,7002 192.168.3.11 7004,7005,7006 2:安装Redis 分别在10与11机器上面安装R ...
- springboot测试启动报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
springboot测试启动报错: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you ne ...
- SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...
- JPA实体类注解、springboot测试类、lombok的使用
前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...
- IntelliJ IDEA 2017版 SpringBoot测试类编写
SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git
- springboot 测试
本次测试使用的是springboot 中的测试 1.(对service 的测试)下面的测试.将会启动容器进行测试 @RunWith(SpringRunner.class) @SpringBootTes ...
- 第一个SpringBoot测试实例
1.SpringBoot项目构建:http://start-spring.io 自动化构建SpringBoot项目,保存在本地并解压 2.安装gradle并配置gradle环境 3.配置阿里云ma ...
随机推荐
- Pthon Matplotlib 画图
一.普通绘图 import matplotlib.pyplot as plt import numpy as np # 绘制普通图像 x = np.linspace(-1, 1, 50) y1 = 2 ...
- 带分数|2013年蓝桥杯B组题解析第九题-fishers
带分数 100 可以表示为带分数的形式:100 = 3 + 69258 / 714 还可以表示为:100 = 82 + 3546 / 197 注意特征:带分数中,数字1~9分别出现且只出现一次(不包含 ...
- parent对象
在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...
- jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax
jQuery jQuery介绍 1.jQuery是一个轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方 ...
- 介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码
http://gijgo.com gijgo组件 特点:简易.文档全-虽然是英文的但是清晰易懂可读性强.含示例代码(后端直接用原生.Net C# MVC的哦!非常合.Net开发胃口),网站网速快, ...
- 状压dp的另一种形式
做的那么多都是一些比较怎么说呢,都是在数网格一类的题目之中,这些题目有些有点固定的套路,而一些需要状态压缩的题目呢,则么是真正对状态转移的考验. 这道题呢,被彻底打脸了,以后一定要任性一点一道题做不出 ...
- vue的生命周期(lifecycle)
这边转载一篇文章,个人认为写的不错,代码举了个例子很生动. https://segmentfault.com/a/1190000010336178
- vue脚手架 构建豆瓣App 第一天
课堂笔记: 项目结构分析: 项目入口:index.html(div#app) 全局vue组件:App.vue(template:div#app) 通过相同id的div,index.html与Appvu ...
- 解决采集知乎数据时由于账号被封遗漏的账号重爬问题(python代码)
'''一.最笨的办法了################################################################为了处理由于账号被封而没跑到的问题id进行以下两步 ...
- oracle的undo表空间
undo表空间是Oracle特有的概念.undo表空间中会自动分配undo段,这些undo段用来保存事务中的DML语句的undo信息,也就是来保存数据在被修改之前的值.在rollback,实例恢复(回 ...