Springboot 之 单元测试
来源:互联网 发布:ubuntu拷贝文件夹 编辑:程序博客网 时间:2024/04/30 13:12
本文章来自【知识林】
在Springboot开发过程中会经常用到单元测试,相对写Controller而言,单元测试更为简单方便。
本例子的测试主要是通过单元测试的方式实现上一篇文章《Springboot 之 自定义配置文件及读取配置文件》中的测试。
在pom.xml
中引入Maven依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency>
读取核心配置文件
application.properties
配置文件内容如下:
server.port=9090test.msg=Hello World Springboot!
- 创建测试类
所有测试类均放在src/test
目录下,这里创建MyTest.java
文件:
package com.zslin;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.core.env.Environment;import org.springframework.test.context.junit4.SpringRunner;/** * Created by 钟述林 393156105@qq.com on 2016/10/18 11:25. */@SpringBootTest@RunWith(SpringRunner.class)public class MyTest { @Value("${test.msg}") private String msg; @Test public void testCoreConfig() { System.out.println(msg); } @Autowired private Environment env; @Test public void testCoreConfig2() { System.out.println(env.getProperty("test.msg")); }}
注意
1、 要让一个普通类变成一个单元测试类只需要在类名上加入@SpringBootTest
和@RunWith(SpringRunner.class)
两个注释即可。
2、 在测试方法上加上@Test
注释。
运行testCoreConfig
和testCoreConfig2
方法后均可得到:Hello World Springboot!
读取自定义配置文件
- 创建配置文件
在resources/config
依然创建名为my-web.properties
的配置文件,内容如下:
web.name=zslinweb.version=V 1.0web.author=393156105@qq.com
- 创建配置文件管理类
package com.zslin.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * Created by 钟述林 393156105@qq.com on 2016/10/18 11:44. */@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web")@Componentpublic class MyConfig { private String name; private String version; private String author; public String getAuthor() { return author; } public String getName() { return name; } public String getVersion() { return version; } public void setAuthor(String author) { this.author = author; } public void setName(String name) { this.name = name; } public void setVersion(String version) { this.version = version; }}
和上一篇文档中的没什么两样。
- 创建测试类
package com.zslin;import com.zslin.config.MyConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/** * Created by 钟述林 393156105@qq.com on 2016/10/18 11:44. */@RunWith(SpringRunner.class)@SpringBootTestpublic class MyConfigTest { @Autowired private MyConfig myConfig; @Test public void testConfig() { System.out.println("webName: "+myConfig.getName()+ ", webVersion: "+ myConfig.getVersion()+", webAuthor: "+myConfig.getAuthor()); }}
运行testConfig
将得到结果:webName: zslin, webVersion: V 1.0, webAuthor: 393156105@qq.com
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study03
本文章来自【知识林】
0 0
- Springboot 之 单元测试
- SpringBoot之添加单元测试
- 5.Springboot 之 单元测试
- springboot 之单元测试:MockBean
- SpringBoot进阶之单元测试
- SpringBoot学习之--单元测试(八)-2
- SpringBoot之构建Restful API与单元测试
- SpringBoot单元测试
- SpringBoot单元测试
- SpringBoot单元测试
- SpringBoot-单元测试
- SpringBoot-单元测试
- springboot【3】web开发之构建RESTful API与单元测试
- springBoot单元测试之1.4版本前后的变化
- SpringBoot学习之整合SSM+pagehelper+druid+单元测试
- SpringBoot Junit单元测试
- Springboot 跑单元测试
- SpringBoot junit单元测试
- NRF51822开发中常见问题和解决办法汇总----持续添加
- Springboot 之 自定义配置文件及读取配置文件
- xstream与resttemplate的联合使用
- 第八周项目3-顺序串算法
- 并发编程基础
- Springboot 之 单元测试
- 从外网远程控制家里的电视盒子,无需设置路由
- 【第六周 项目6-求解8皇后问题的程序】
- Windows 7 Ultra Edit 运行错误 解决方法
- Springboot 之 多配置文件
- POJ 2296Map Labeler 2-sat + 矩阵相交
- Springboot 之 Hibernate自动建表(Mysql)
- 第八周项目2-建立链串的算法库
- 网络知识点(TCP与UDP,get与post等)