spring-boot 速成(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主程序对应的类,大家根据自己的实际类名进行替换,并非测试类本身。
spring-boot 速成(11) - 单元测试的更多相关文章
- 【spring boot】10.spring boot下的单元测试
spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...
- Spring Boot实战之单元测试
Spring Boot实战之单元测试 本文介绍使用Spring测试框架提供的MockMvc对象,对Restful API进行单元测试 Spring测试框架提供MockMvc对象,可以在不需要客户端-服 ...
- Spring Boot 1.4 单元测试
在1.3中单元测试这样子的类似代码: // SpringJUnit支持,由此引入Spring-Test框架支持! @RunWith(SpringJUnit4ClassRunner.class) // ...
- Spring boot Junit Test单元测试
Spring boot 1.40 JUnit 4 需要依赖包 spring-boot-starter-test.spring-test 建立class,加上如下注解,即可进行单元测试,别的帖子里说要加 ...
- 83. Spring Boot 1.4单元测试【从零开始学Spring Boot】
在[27. Spring Boot Junit单元测试]中讲过1.3版本的单元测试方式,这里说说1.4和1.3有什么区别之处? 在1.3中单元测试这样子的类似代码: //// SpringJUnit支 ...
- Spring Boot 入门之单元测试篇(五)
博客地址:http://www.moonxy.com 一.前言 JUnit 是一个由 Java 语言编写的开源的回归测试(回归测试是指重复以前全部或部分的相同测试)框架,由Erich Gamma 和 ...
- spring boot 集成 mybatis 单元测试Dao层 控制台报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
最近帮同学做毕业程序,采用后端spring boot + mybatis + H2,将框架搭好进行各层的单元测试时,在dao层就出现了错,如图 于是在网上找各种资料,有的说是xml文件和接口没有一一对 ...
- 【maven】【spring boot】【单元测试】 使用controller 执行单元测试类
存在这样一个场景: 当项目启动时间过长,又没办法缩短的时候,写单元测试就是一个十分耗时的工作, 这工作不在于使用编写代码,而在于每次run junit test 都需要完整启动一次项目,白白浪费宝贵的 ...
- 【spring boot】11.spring-data-jpa的详细介绍和复杂使用
==================================================================================================== ...
随机推荐
- html5 canvas显示文字(写上5个字,纪念我那刚刚逝去的爱情)
<script type="text/javascript"> window.addEventListener('load',eventWindowLoaded,fal ...
- sql查询语句优化
http://www.cnblogs.com/dubing/archive/2011/12/09/2278090.html 最近公司来一个非常虎的dba 10几年的经验 这里就称之为蔡老师吧 在征得 ...
- python之celery使用详解(二)
前言 前面我们了解了celery的基本使用后,现在对其常用的对象和方法进行分析. Celery对象 核心的对象就是Celery了,初始化方法: class Celery(object): def __ ...
- csfb
SELECT CSDBTOGSMALLSuccessRate,CSFBTOGSMMODelay,CSFBTOGSMMODropRate,CSFBTOGSMMOFRStartCount,CSFBTOGS ...
- 从Dying gasp功能看Linux的响应速度(zhuan)
转自https://blog.csdn.net/qq_20405005/article/details/77967358 前一阵子在做dying gasp功能测试,过程中恰好测试到了Linux的响应速 ...
- XShell 使用方法
XShell是一款Windows下非常优秀的远程连接Linux主机的工具,是平常使用不可缺少的工具.复制和粘贴由于在linux的Shell下,Ctrl+c是中断当前指令,这个快捷键和win系统下的复制 ...
- asp.net 文件下载显示中文名称
protected void Page_Load(object sender, EventArgs e) { string guid = Request.QueryString[& ...
- 驱动程序vmci.sys版本不正确。请尝试重新安装 VMware
今天在测试一台服务器,安装在虚拟机里面,但是发现在安装后,重启了一下电脑,出现了这个错误: 无法获取 vmci 驱动程序版本: 句柄无效.驱动程序 vmci.sys 版本不正确.请尝试重新安装 VMw ...
- [转]solver优化方法
原文地址:http://www.cnblogs.com/denny402/p/5074212.html 到目前为止,caffe总共提供了六种优化方法: Stochastic Gradient Desc ...
- MongoDB 进阶模式设计
原文链接:http://www.mongoing.com/mongodb-advanced-pattern-design 12月12日上午,TJ在开源中国的年终盛典会上分享了文档模型设计的进阶技巧,就 ...