当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
Spring boot(十二):Spring boot 如何测试、打包、部署
生活随笔
收集整理的这篇文章主要介绍了
Spring boot(十二):Spring boot 如何测试、打包、部署
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
博文引用:springboot(十二):springboot如何测试打包部署
开发阶段
单元测试
Spring boot对单元测试的支持已经很完善了。
1 在pom包中添加Spring-boot-starter-test包引用
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>2 开发测试类
头部添加:@RunWith(SpringRunner.class)和@SpringBootTest注解,在测试方法上添加 测试方法@Test即可。
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests {@Testpublic 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);引入MockMvc支持对Controller层的测试,简单实例:
public class HelloControlerTests {private MockMvc mvc;//初始化执行@Beforepublic void setUp() throws Exception {mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();}//验证controller是否正常响应并打印返回结果@Testpublic void getHello() throws Exception {mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();}//验证controller是否正常响应并判断返回结果是否正确@Testpublic 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>添加以上配置后,项目就支持了热部署,非常方便集成测试。
投产上线
总结
以上是生活随笔为你收集整理的Spring boot(十二):Spring boot 如何测试、打包、部署的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 怎么查看历史价格唯品会怎么查看历史价格
- 下一篇: Sprng boot(十三):Sprin