一、添加依赖项:

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主程序对应的类,大家根据自己的实际类名进行替换,并非测试类本身。

最新文章

  1. 总结六条对我们学习Linux系统有用的忠告
  2. paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中总结
  3. 又一个悬而未决的bug被解决
  4. mysql存储过程出参入参,sqlserver很熟悉的一件事到mysql,捣鼓了大半天。记录一下提醒自己。勿看
  5. Linux some command(continue...)
  6. 解决Win8.1 / Win Server 2012 r2 下安装 Visual Studio 时一直要求重新启动的问题(原创)
  7. INV Close Period &amp; GL Import Journal &gt; DML tables
  8. 未能从程序集“WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35“ 中加载“System.Windows.SplashSceen”
  9. Blogger建立Blog部落格​​ - Blog透视镜
  10. Kendo UI开发教程(27): 移动应用开发简介
  11. Poi2006 Palindromes
  12. thinkphp导出csv格式的表格
  13. linux下用iptables做本机端口转发方法(转载)
  14. Spring Security入门(3-2)Spring Security对接用户的权限系统
  15. [Codeforces 448C]Painting Fence
  16. XSS简介
  17. 牛客网练习赛t2(线段树)
  18. Python 爬虫 --- urllib
  19. 【Python爬虫实战】Scrapy框架的安装 搬运工亲测有效
  20. IDEA &amp; Android Studio换主题背景

热门文章

  1. javascript有关this的那些事(某渣提出的问题)
  2. 【LibreOJ】#6392. 「THUPC2018」密码学第三次小作业 / Rsa 扩展欧几里得算法
  3. iOS 远程推送注册的小问题
  4. Codeforces 238 div2 B. Domino Effect
  5. linux中使用corntab和shell脚本自动备份nginx日志,按天备份
  6. linux串口驱动分析【转】
  7. 从消费者角度评估RestFul的意义
  8. python3.3中print换行
  9. URL传递的参数是UTF-8编码,在打开的页面正常显示(GB2312)的方法
  10. 今天我碰到了由于web.xml文件表头信息导致润乾报表启动失败的问题,解决方案如下