博文引用:springboot(十二):springboot如何测试打包部署
Spring boot对单元测试的支持已经很完善了。
头部添加:@RunWith(SpringRunner.class)和@SpringBootTest注解,在测试方法上添加 测试方法@Test即可。
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Test public void hello() { System.out.println("hello world"); } }实际使用中,可以按照项目的正常使用注入dao层或是service层代码进行测试验证,spring-boot-starter-test提供很多基础用法,更难得的是增加了对Controller层测试的支持。
//简单验证结果集是否正确 Assert.assertEquals(3, userMapper.getAll().size()); //验证结果集,提示 Assert.assertTrue("错误,正确的返回值为200", status == 200); Assert.assertFalse("错误,正确的返回值为200", status != 200);单元测试时验证你代码的第一道屏障,要养成每写一部分代码就进行单元测试的习惯,不要等到全部集成后进行测试,集成后因为更关注整体运行效果,很容易遗漏掉底层的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>添加以上配置后,项目就支持了热部署,非常方便集成测试。