一、单元测试

在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了。

1、在pom包中添加spring-boot-starter-test包引用

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

2、开发测试类

以最简单的helloworld为例,在测试类的类头部需要添加:@RunWith(SpringRunner.class)@SpringBootTest注解,在测试方法的顶端添加@Test即可,最后在方法上点击右键run就可以运行。

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class MailServiceTest {
  4.  
  5. @Autowired
  6. private MailService mailService;
  7.  
  8. @Test
  9. public void testSimpleMail() throws Exception {
  10. mailService.sendSimpleMail("1249736987@qq.com", "test simple mail", " zmc");
  11. }
  12.  
  13. @Test
  14. public void hello() {
  15. System.out.println("hello world,zmc");
  16. }
  17.  
  18. }

实际使用中,可以按照项目的正常使用去注入dao层代码或者是service层代码进行测试验证,spring-boot-starter-test提供很多基础用法,更难得的是增加了对Controller层测试的支持。

  1. //简单验证结果集是否正确
  2. Assert.assertEquals(3, userMapper.getAll().size());
  3.  
  4. //验证结果集,提示
  5. Assert.assertTrue("错误,正确的返回值为200", status == 200);
  6. Assert.assertFalse("错误,正确的返回值为200", status != 200);

引入了MockMvc支持了对Controller层的测试,简单示例如下:

  1. public class HelloControlerTests {
  2.  
  3. private MockMvc mvc;
  4.  
  5. //初始化执行
  6. @Before
  7. public void setUp() throws Exception {
  8. mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
  9. }
  10.  
  11. //验证controller是否正常响应并打印返回结果
  12. @Test
  13. public void getHello() throws Exception {
  14. mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
  15. .andExpect(MockMvcResultMatchers.status().isOk())
  16. .andDo(MockMvcResultHandlers.print())
  17. .andReturn();
  18. }
  19.  
  20. //验证controller是否正常响应并判断返回结果是否正确
  21. @Test
  22. public void testHello() throws Exception {
  23. mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
  24. .andExpect(status().isOk())
  25. .andExpect(content().string(equalTo("Hello World")));
  26. }
  27.  
  28. }

单元测试是验证你代码第一道屏障,要养成每写一部分代码就进行单元测试的习惯,不要等到全部集成后再进行测试,集成后因为更关注整体运行效果,很容易遗漏掉代码底层的bug.

二、集成测试

整体开发完成之后进入集成测试,spring boot项目的启动入口在 Application类中,直接运行run方法就可以启动项目,但是在调试的过程中我们肯定需要不断的去调试代码,如果每修改一次代码就需要手动重启一次服务就很麻烦,spring boot非常贴心的给出了热部署的支持,很方便在web项目中调试使用。

pom需要添加以下的配置:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-devtools</artifactId>
  5. <optional>true</optional>
  6. </dependency>
  7. </dependencies>
  8.  
  9. <build>
  10. <plugins>
  11. <plugin>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-maven-plugin</artifactId>
  14. <configuration>
  15. <fork>true</fork>
  16. </configuration>
  17. </plugin>
  18. </plugins>
  19. </build>

添加以上配置后,项目就支持了热部署,非常方便集成测试。

springboot测试的更多相关文章

  1. springboot测试、打包、部署

    本文使用<springboot集成mybatis(一)>项目,依次介绍springboot测试.打包.部署. 大多数朋友是做后端的,也就是为其他系统或者前端UI提供Rest API服务. ...

  2. SpringBootTest单元测试实战、SpringBoot测试进阶高级篇之MockMvc讲解

    1.@SpringBootTest单元测试实战 简介:讲解SpringBoot的单元测试 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> &l ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. JPA实体类注解、springboot测试类、lombok的使用

    前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...

  7. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  8. springboot 测试

    本次测试使用的是springboot 中的测试 1.(对service 的测试)下面的测试.将会启动容器进行测试 @RunWith(SpringRunner.class) @SpringBootTes ...

  9. 第一个SpringBoot测试实例

    1.SpringBoot项目构建:http://start-spring.io   自动化构建SpringBoot项目,保存在本地并解压 2.安装gradle并配置gradle环境 3.配置阿里云ma ...

随机推荐

  1. officewebapps 服务器部署问题

    officewebapps 服务器部署问题 部署文档 http://technet.microsoft.com/zh-cn/library/jj219455 New-OfficeWebAppsFarm ...

  2. wpgcms---导航高亮显示

    在使用wpgcms做项目的时候,有时候三级栏目默认是没有高亮显示的一级导航的,例如:文章详情页要对应的文章栏目进行高亮显示,三级单篇页要对应栏目是高亮显示.具体做法是: 首先看获取导航的方式: {% ...

  3. C++服务器下载文件的两种方式

    #include <afxinet.h>#include "wininet.h" #pragma comment( lib, "wininet.lib&quo ...

  4. 10.13 Django随笔

    2018-10-13 14:20:59 越努力,越幸运! 永远不要高估自己! Django的渲染是在render()时候渲染的,然后把字符串传给浏览器 Django请求流程, 跨域 参考链接: htt ...

  5. 转载:浅析@PathVariable 和 @RequestParam

    在网上看了一篇很好的文章,讲的很清楚明了,说到了点子上(转自:https://blog.csdn.net/chuck_kui/article/details/55506723): 首先 上两个地址: ...

  6. python面向对象高级:枚举

    在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数.这两种类型经常(但不总是)重叠. 枚举是一个被命名的整型常数的集合,枚举在日常生活中很常见,例 ...

  7. Mac开发博客摘录

    https://blog.csdn.net/wangyouxiang/article/details/17855255 https://www.cocoacontrols.com/controls?p ...

  8. Elastic数据迁移方法及注意事项

    需求 ES集群Cluster_A里的数据(某个索引或某几个索引),需要迁移到另外一个ES集群Cluster_B中. 环境 Linux:Centos7 / Centos6.5/ Centos6.4Ela ...

  9. linux状态及原理全剖析

    Table of Contents 1 linux 1.1 proc filesystem 1.1.1 /proc 1.1.1.1 /proc/meminfo 1.1.1.2 /proc/stat 1 ...

  10. centos7配置vsftpd

    ftp服务器192.168.1.198 1.关闭selinux.关闭防火墙 2.yum -y install vsftpd* 3.开启服务,通过浏览器访问ftp://192.168.1.198,匿名用 ...