一、单元测试

在开发阶段的时候最重要的是单元测试了,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测试的更多相关文章

  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. Linux模拟网络延迟、丢包等

    一.工具介绍 1.netem netem 是 Linux 2.6 及以上内核版本提供的一个网络模拟功能模块.该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带宽.传输延迟.丢 ...

  2. cmus 命令行播放器使用

    安装 sudo eopkg it cmus 启动 cmus 设置输出 :set output_plugin=pulse 导入本地音乐 :add /home/your_username/Music 查看 ...

  3. STL之vector,deque学习实例

    ``` #include<iostream> #include<algorithm> #include<ctime> #include<vector> ...

  4. Spark RDD 默认分区数量 - repartitions和coalesce异同

    RDD.getNumPartitions()方法可以获得一个RDD分区数量, 1.默认由文件读取的话,本地文件会进行shuffle,hdfs文件默认会按照dfs分片来设定. 2.计算生成后,默认会按照 ...

  5. 【mysql】---php链接数据库---【巷子】

    一.创建public文件 <?php //第一件事情连接数据库 header("content-type:text/html;charset=utf8"); //服务器地址 ...

  6. python3.6利用pyinstaller模块打包程序为.exe可执行程序

    步骤: 1.安装pyinstaller模块:(必须在联网情况下进行) 操作原理: python3.6已经自带了pip,所以只需要在cmd中执行 pip install pyinstaller 就可以安 ...

  7. ASP.NET Core 2.0 Preview 1 中贴心的新特性

    西雅图时间5月10日,微软在 Build 2017 大会上发布了 ASP.NET Core 2.0 Preview 1 ( 详见 Announcing ASP.NET 2.0.0-Preview1 a ...

  8. [No0000C2]WPF 数据绑定的调试

    )DataBinding的表达式无效时,跟踪Debug的输出信息来查找原因(2)DataBinding的表达式有效,但是数据和期望值不同,此时可以在Converter中断点调试 :在VS输出窗口跟踪信 ...

  9. 1.7Oo局部变量和成员变量执行顺序

    import java.util.Scanner; public class booleann { private float fWidth; private float fHeight; void ...

  10. iOS RSA非对称加密测试流程

    非对称加密需要两把钥匙:公钥和秘钥. 单向加密:一般情况下服务器会持有秘钥和公钥,那该怎么使用呢?以注册场景为例,最初服务器持有公钥和密钥. 用户注册时不是直接发送用户名,密码,验证码等明文信息给服务 ...