单元测试(一)——SpringBoot建立单元测试
在Spring项目中,对于controller、service、dao各层都需要建立单元测试项。对应不同的分层,我们可以使用junit和mock不同的方式。然而有些情况会需要启动spring容器来测试业务逻辑在容器内能否正常运行,针对此情况可参考如下单元测试方式。
SpringBoot增加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test<artifactId> <scope>test</scope> </dependency>
建立测试基类
- 新建JUnitBaseTest.java
package com.lucas.device; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.util.StopWatch; import lombok.extern.slf4j.Slf4j; /** * <Description> <br> * * @author xubin<br> * @version 1.0<br> * @taskId <br> * @CreateDate 2018年9月27日 <br> */ @Slf4j @RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration public class JUnitBaseTest { /** * sw 计时器<br> */ public static StopWatch sw = null; /** * Description: <br> * * @author xubin<br> * @taskId <br> * @throws java.lang.Exception <br> */ @BeforeClass public static void setUpBeforeClass() throws Exception { sw = new StopWatch(); } /** * Description: <br> * * @author xubin<br> * @taskId <br> * @throws java.lang.Exception <br> */ @AfterClass public static void tearDownAfterClass() throws Exception { log.info("**************************************************************"); log.info("单元测试计时统计:{}", sw.prettyPrint()); log.info("**************************************************************"); } /** * Description: <br> * * @author xubin<br> * @taskId <br> * @throws java.lang.Exception <br> */ @Before public void setUp() throws Exception { log.info("开始测试-----------------"); } /** * Description: <br> * * @author xubin<br> * @taskId <br> * @throws java.lang.Exception <br> */ @After public void tearDown() throws Exception { sw.stop(); log.info("测试结束-----------------"); } }
- @BeforeClass 基类执行前触发
- @AfterClass 基类执行后触发
- @Before 每个@Test方法调用前触发
- @After 每个@Test方法调用后触发
测试Case
- service层单元测试示例 DeviceInfoTest.java
package com.lucas.device.service; import static org.junit.Assert.fail; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.springframework.data.redis.core.StringRedisTemplate; import com.lucas.core.common.PageInfo; import com.lucas.device.JUnitBaseTest; import com.lucas.device.bo.DeviceInfoBO; import com.lucas.device.entity.DeviceInfoEntity; import com.lucas.device.entity.cache.DeviceCache; import com.phlico.common.framework.tool.unique.IdWorkerUtil; import com.phlico.common.framework.web.util.JsonUtil; import lombok.extern.slf4j.Slf4j; /** * <Description> <br> * * @author xubin<br> * @version 1.0<br> * @taskId <br> * @CreateDate Nov 4, 2018 <br> */ @Slf4j public class DeviceInfoTest extends JUnitBaseTest { /** * deviceInfoService <br> */ @Resource(name = "deviceInfoServiceImpl2") private DeviceInfoService deviceInfoService; /** * deviceRedisTemplate <br> */ @Resource private StringRedisTemplate stringRedisTemplate; /** * appKeyArr <br> */ public static final String[] appKeyArr = { "2e2b380a56e7464aa678294c2c345545", "e2c85a1d245844fd9bdb14f0d0fc868a", "fa58f5306eb64ce094fe65fec6261587" }; /** * Description: <br> * * @author xubin<br> * @taskId <br> * <br> */ @Ignore @Test public void saveTest() { log.info("设备保存"); sw.start("设备保存"); try { DeviceInfoBO deviceInfoBO = new DeviceInfoBO(); deviceInfoBO.setAppKey("2e2b380a56e7464aa678294c2c345545"); deviceInfoBO.setDeviceName("test2"); deviceInfoBO.setDeviceCode("EE-FF-GG"); deviceInfoBO.setDeviceDesc("测试设备1"); deviceInfoBO.setDeviceType("bcb0ca4ef0eb463aa76a5bc0ccee1b85"); deviceInfoBO.setDeviceState("b9cf9e01a5534487919a77f21c34c93c"); deviceInfoBO.setDeviceIpv4("192.168.109.12"); deviceInfoBO.setDevicePort(8088); deviceInfoBO.setDeviceMac("ED:AS:ZQ:QS"); deviceInfoBO.setProtocol(0); deviceInfoService.save(deviceInfoBO); } catch (Exception e) { fail("设备保存异常"); } } /** * Description: <br> * * @author xubin<br> * @taskId <br> * <br> */ @Ignore @Test public void updateTest() { log.info("设备修改"); sw.start("设备修改"); try { DeviceInfoBO deviceInfoBO = new DeviceInfoBO(); deviceInfoBO.setAppKey("2e2b380a56e7464aa678294c2c345545"); deviceInfoBO.setDeviceName("测试设备-" + appKeyArr[(int) (Math.random() * appKeyArr.length)]); deviceInfoBO.setDeviceCode("EE-FF-GG"); deviceInfoBO.setDeviceDesc("测试设备2"); deviceInfoBO.setDeviceState("b9cf9e01a5534487919a77f21c34c93c"); deviceInfoBO.setDeviceIpv4("192.168.109.122"); deviceInfoBO.setDevicePort(8089); deviceInfoBO.setDeviceMac("ED:AS:ZQ:QS:DA"); deviceInfoBO.setProtocol(0); deviceInfoService.update(deviceInfoBO); } catch (Exception e) { fail("设备修改异常"); } } /** * Description: <br> * * @author xubin<br> * @taskId <br> * <br> */ @Ignore @Test public void delDeviceByCodesTest() { log.info("设备删除"); sw.start("设备删除"); try { deviceInfoService.delDevicesByCode("2e2b380a56e7464aa678294c2c345545", "EE-FF-GG", null, null, null); } catch (Exception e) { fail("设备删除异常"); } } /** * Description: <br> * * @author xubin<br> * @taskId <br> * <br> */ @Ignore @Test public void delDeviceByIdsTest() { log.info("按id删除设备"); sw.start("按id删除设备"); try { deviceInfoService.delete("2e2b380a56e7464aa678294c2c345545", "d99b77637c1e421db89ac89579d43eef,00d2105433d94106a88596f275e58d41", null, null, new Date()); } catch (Exception e) { fail("按id删除设备异常"); } } /** * Description: <br> * * @author xubin<br> * @taskId <br> * <br> */ @Ignore @Test public void queryEntityListByPageTest() { log.info("分页查询设备"); sw.start("分页查询设备"); try { DeviceInfoBO bo = new DeviceInfoBO(); bo.setAppKey("2e2b380a56e7464aa678294c2c345545"); PageInfo<DeviceInfoBO> list = deviceInfoService.queryEntityListByPage(bo, 1, 15); log.info(JsonUtil.getSucc4data(list)); Assert.assertNotEquals(null, list); } catch (Exception e) { fail("分页查询设备异常"); } } /** * Description: <br> * * @author xubin<br> * @taskId <br> * <br> */ @Ignore @Test public void queryDeviceInfoByCacheTest() { log.info("查询缓存所有设备"); sw.start("查询缓存所有设备"); try { String appKey = "fa58f5306eb64ce094fe65fec6261587"; String deviceCodes = "577984734685110272"; List<DeviceCache> list = deviceInfoService.queryDeviceInfoByCache(appKey, deviceCodes); log.info(JsonUtil.getSucc4data(list)); Assert.assertNotEquals(null, list); } catch (Exception e) { fail("查询缓存所有设备异常"); } } }
- @Ignore 忽略测试方法
- @Test 执行单元测试的方法
使用StopWatch可以统计单元测试类执行总时间以及每个@Test方法执行时间。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
webpack4配置详解之逐行逐句分析
前言 经常会有群友问起webpack、react、redux、甚至create-react-app配置等等方面的问题,有些是我也不懂的,慢慢从大家的相互交流中,也学到了不少。 今天就尝试着一起来聊聊Webpack吧,旨在帮大家加深理解、新手更容易上路,都能从0到1搭建配置自定属于自己的脚手架,或对已封装好的脚手架有进一步的巩固,接下来苏南会详细讲解webpack中的每一个配置字段的作用(部分为webpack4新增)。 近两年,前端一直在以一个高速持续的过程发展,常常会有网友在调侃老了、学不动了, 虽是在调侃却又间接阐述着无奈,迫于生活的压力,不得不提速前行, 因为没有谁为你而停留,公司不会、社会不会、同伴不会……,停下可能将意味着淘汰 —— 理想很丰满,现实很骨感,所以让我们一起进步,共同加薪,奋斗吧骚年,加油。。 ~~吐槽过了,接着聊正事~~。 原谅我控制不住自己,想问下各位,昨天刚刚过去的双十一你脱单了吗? 人生若只如初见,何事秋风悲画扇; 等闲变却故人心,却道故人心易变; 骊山语罢清宵半,夜雨霖铃终不怨。 各位大佬早安,这里是@IT·平头哥联盟,我是首席填坑官∙苏南,用心分享 ...
- 下一篇
二, 跨语言微服务框架 - Istio环境搭建
当我们知道Istio是一个好东西,能够帮助我们快速实现微服务化中的一些关键节点,那么下一步就需要考虑怎么使用Istio了,Istio现在版本是和Kubernetes强关联在一起的,如果大家还不是太了解Kubernetes可以先从笔者的文章中了解,通过Kubernetes生态Istio可以非常方便的进行部署和使用。 附上: 喵了个咪的博客:w-blog.cn Istio官方地址:https://preliminary.istio.io/zh Istio中文文档:https://preliminary.istio.io/zh/docs/ PS : 此处基于当前最新istio版本1.0.3版本进行搭建和演示 一. Kubernetes准备工作 搭建部署Istio时需要先准备好Kubernetes环境,笔者这边使用的是Rancher进行环境的搭建,可以参考笔者的以下博文: Docker应用容器引擎介绍与搭建 - 喵了个咪博客空间 - 开源中国 Kubernetes(二) - 使用Rancher部署K8S集群(搭建Rancher) - 喵了个咪博客空间 - 开源中国 Kubernetes(三) ...
相关文章
文章评论
共有0条评论来说两句吧...