一、添加依赖项:

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) - 单元测试的更多相关文章

  1. 【spring boot】10.spring boot下的单元测试

    spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...

  2. Spring Boot实战之单元测试

    Spring Boot实战之单元测试 本文介绍使用Spring测试框架提供的MockMvc对象,对Restful API进行单元测试 Spring测试框架提供MockMvc对象,可以在不需要客户端-服 ...

  3. Spring Boot 1.4 单元测试

    在1.3中单元测试这样子的类似代码: // SpringJUnit支持,由此引入Spring-Test框架支持! @RunWith(SpringJUnit4ClassRunner.class) // ...

  4. Spring boot Junit Test单元测试

    Spring boot 1.40 JUnit 4 需要依赖包 spring-boot-starter-test.spring-test 建立class,加上如下注解,即可进行单元测试,别的帖子里说要加 ...

  5. 83. Spring Boot 1.4单元测试【从零开始学Spring Boot】

    在[27. Spring Boot Junit单元测试]中讲过1.3版本的单元测试方式,这里说说1.4和1.3有什么区别之处? 在1.3中单元测试这样子的类似代码: //// SpringJUnit支 ...

  6. Spring Boot 入门之单元测试篇(五)

    博客地址:http://www.moonxy.com 一.前言 JUnit 是一个由 Java 语言编写的开源的回归测试(回归测试是指重复以前全部或部分的相同测试)框架,由Erich Gamma 和 ...

  7. spring boot 集成 mybatis 单元测试Dao层 控制台报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    最近帮同学做毕业程序,采用后端spring boot + mybatis + H2,将框架搭好进行各层的单元测试时,在dao层就出现了错,如图 于是在网上找各种资料,有的说是xml文件和接口没有一一对 ...

  8. 【maven】【spring boot】【单元测试】 使用controller 执行单元测试类

    存在这样一个场景: 当项目启动时间过长,又没办法缩短的时候,写单元测试就是一个十分耗时的工作, 这工作不在于使用编写代码,而在于每次run junit test 都需要完整启动一次项目,白白浪费宝贵的 ...

  9. 【spring boot】11.spring-data-jpa的详细介绍和复杂使用

    ==================================================================================================== ...

随机推荐

  1. 从零开始编写自己的JavaScript框架(一)

    1. 模块的定义和加载 1.1 模块的定义 一个框架想要能支撑较大的应用,首先要考虑怎么做模块化.有了内核和模块加载系统,外围的模块就可以一个一个增加.不同的JavaScript框架,实现模块化方式各 ...

  2. Java 多线程(Thread) 同步(synchronized) 以及 wait, notify 相关 [实例介绍]

    场景描述 有一家很大的商场,在某市有几个商品配送中心,并有几家分店,这家商场经营很多的商品,平时运营情况是这样的: 根据各分店的商品销售情况,给分店配送相应需求量的商品:并上架到分店指让的位置,供客户 ...

  3. Linux - awk 文本处理工具四

    awk 常用示例 获取本机 IP 方式 /sbin/ifconfig |awk -v RS="Bcast:" '{print $NF}'|awk -F: '/addr/{print ...

  4. 【SVN】SVN的trunk、branches、tag的使用以及分支的概念

    SVN命令参考:   https://www.cnblogs.com/wlsxmhz/p/5775393.html svn的存储结构一般建议在根目录下建立trunk.branches.tags这三个文 ...

  5. 在Windows环境中利用Responder工具窃取NTLMv2哈希

    在Windows环境中利用Responder工具窃取NTLMv2哈希 翻译自:https://github.com/incredibleindishell/Windows-AD-environment ...

  6. 006使用Grafana展示时间序列数据

    简介 Grafana是一个独立运行的系统,内置了Web服务器.它可以基于仪表盘的方式来展示.分析时间序列数据. Grafana支持多种数据源,例如:Graphite.OpenTSDB.InfluxDB ...

  7. python文件操作及格式化输出

    1 文件与IO 1.1读写文本数据 读写各种不同的文本数据,如ASCII,UTF-8,UTF-9编码等. 使用带有rt模式的open()函数读取文本文件. 例如: with open('db', 'r ...

  8. phpMyAdmin setup.php脚本的任意PHP代码注入漏洞

    phpMyAdmin (/scripts/setup.php) PHP 注入代码 此漏洞代码在以下环境测试通过:      phpMyAdmin 2.11.4, 2.11.9.3, 2.11.9.4, ...

  9. JavaScript——HashMap实现

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接博客园蜗牛 cnblogs.com\tdws . 首先提供一种获取hashCode的方法,是一种比较受欢迎的方式,该方法参照了一位园友的 ...

  10. explain的使用

    MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化. mysql. row ************ ...