spring-boot 速成(11) - 单元测试
2024-09-21 00:54:11
一、添加依赖项:
testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE'
二、单元测试代码示例
import cn.mwee.winpos.cloud.admin.service.demo.DemoServiceProvider;
import cn.mwee.winpos.cloud.admin.service.demo.impl.HealthCheckServiceImpl;
import cn.mwee.winpos.cloud.admin.service.dto.common.DataResult;
import cn.mwee.winpos.cloud.admin.service.dto.common.PingResponse;
import cn.mwee.winpos.common.utils.json.JsonUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner; /**
* Created by 菩提树下的杨过 on 13/08/2017.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoServiceProvider.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class DemoServiceTests { Logger log = LoggerFactory.getLogger(DemoServiceTests.class); @Autowired
HealthCheckServiceImpl healthCheckService; @Autowired
JsonUtil jsonUtil; @Test
public void ping() {
DataResult<PingResponse> data = healthCheckService.ping();
log.info(jsonUtil.format(jsonUtil.toJson(data)));
} }
注意一下,最上面几个注解的写法,网上很多文章的示例都是低版本的注解,在1.4版本以后,有些注解已经废弃,高版本的spring-boot,请参考上面的正确写法。如果想切换profile,比如:想切换到dev环境 ,把@ActiveProfiles("test") 里面的test改成dev即可;另外@SpringBootTest(classes = DemoServiceProvider.class...)这里的classes,指SpringBootApplication主程序对应的类,大家根据自己的实际类名进行替换,并非测试类本身。
最新文章
- 总结六条对我们学习Linux系统有用的忠告
- paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中总结
- 又一个悬而未决的bug被解决
- mysql存储过程出参入参,sqlserver很熟悉的一件事到mysql,捣鼓了大半天。记录一下提醒自己。勿看
- Linux some command(continue...)
- 解决Win8.1 / Win Server 2012 r2 下安装 Visual Studio 时一直要求重新启动的问题(原创)
- INV Close Period &; GL Import Journal >; DML tables
- 未能从程序集“WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35“ 中加载“System.Windows.SplashSceen”
- Blogger建立Blog部落格​​ - Blog透视镜
- Kendo UI开发教程(27): 移动应用开发简介
- Poi2006 Palindromes
- thinkphp导出csv格式的表格
- linux下用iptables做本机端口转发方法(转载)
- Spring Security入门(3-2)Spring Security对接用户的权限系统
- [Codeforces 448C]Painting Fence
- XSS简介
- 牛客网练习赛t2(线段树)
- Python 爬虫 --- urllib
- 【Python爬虫实战】Scrapy框架的安装 搬运工亲测有效
- IDEA &; Android Studio换主题背景
热门文章
- javascript有关this的那些事(某渣提出的问题)
- 【LibreOJ】#6392. 「THUPC2018」密码学第三次小作业 / Rsa 扩展欧几里得算法
- iOS 远程推送注册的小问题
- Codeforces 238 div2 B. Domino Effect
- linux中使用corntab和shell脚本自动备份nginx日志,按天备份
- linux串口驱动分析【转】
- 从消费者角度评估RestFul的意义
- python3.3中print换行
- URL传递的参数是UTF-8编码,在打开的页面正常显示(GB2312)的方法
- 今天我碰到了由于web.xml文件表头信息导致润乾报表启动失败的问题,解决方案如下