当前位置:网站首页>Spock单元测试框架介绍及在美团优选的实践_第四章(Exception异常处理mock方式)
Spock单元测试框架介绍及在美团优选的实践_第四章(Exception异常处理mock方式)
2022-07-08 00:54:00 【灰太狼yyds】
Spock单元测试框架介绍
文章目录
前言
什么是单元测试,其实单元测试是在我们开发完功能之后,对于业务逻辑用到了第三方的调用,或者对数据库的操作等动作不能够支持的情况下,对业务功能进行以方法为颗粒度进行测试业务逻辑是否与预期的效果数据相等。在很多的一线互联网公司研发人员开发完功能之后都要进行单元测试的,保证行覆盖率、分支覆盖率符合标注之后,才可以上线,以减少线上事故发生。
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是Spock单元测试框架?
帮助大家提高开发测试效率。
Spock是什么?和JUnit、jMock有什么区别?
Spock是国外一款优秀的测试框架,基于BDD(行为驱动开发) 思想,功能强大,能够让我们的单元测试代码规范化,结构层次清晰。Spock结合Groovy动态语言的特点以及自身提供的各种标签
种通用、简单、结构化的描述语言,让编写测试代码更加高效和简洁,
官方的介绍如下:https://spockframework.org
二、使用步骤
1.引入maven依赖
<!-- spock单元测试框架 -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.0-groovy-3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.4.15</version>
</dependency>
2.创建GoodsService订单业务类进行单测
说明:
1、hashMap为模拟从数据库查询的订单信息,没有触发IO操作
2、根据请求的订单编号信息goodsInfo,校验数据库是否存在订单记录,有则更新,无则抛出客户端异常信息(我们重点要测试的)。
3.创建Groovy.class类
new—>Groovy Class类
4.创建GoodsServiceSpec.java
创建GoodsServiceSpec类对,queryGoods()方法进行单元测试,命名严格要求:要测的类名+Spec
说明:
1、创建类之后需要继承extends Specification类
3、thrown()捕获异常信息,如果你测试的方法有异常但是没有抛出来,thrown()获取不到异常信息是会报错的,要注意!
4、expectedException 异常信息类型,空指针啥的。。。
expectedMessage 异常信息,包括描述信息
5、创建测试方法格式如下
/** * 灰太狼想吃铁锅炖。。。。。。。 * 这里注意;10088订单信息是不存在的,要测的就是抛出她的异常信息。。 */
class ceshiSpec extends Specification{
def goodsService = new GoodsService()
def "updateGoods Exception"() {
given: "测试mock,Exception异常情况测试方法"
def mapList = [10086, 10087, 10088]
and: "mock方法"
//when 标签代表要测试方法的入口,只能又一个方法
when: "测试方法"
def response = goodsService.updateGoods(mapList)
//then 对结果进行验证
then: "校验结果"
def exception = thrown(expectedException)
exception.getMessage() == expectedMessage
where:
input || expectedException | expectedMessage
"测试异常情况" || ClientException | "订单信息不存在,单号为:10088请查验!"
}
}
实际效果与预期效果一致,表示测试通过。
实际效果与预期效果不一致,表示测试失败
5.groowy简单语法
where以标准格式开使提供集合
when调用指定方法函数
then: 故出断高裝达式
expect 期站的行为,shen-then的特点
giveni mock单测中指定mock数信
throwin:如果在when方法中地出现异常,则在这个子句中会捕玩到异常并返回
def setup{}:每个训过运行前的启动方法
der cleanup{}:每个加武运行居的清理万法
def setupSpec{}:每个测试前的启动方法
def cleanupSpec{}:服后一介测试运行后的消理方法
总结
以上就是今天要讲的内容,本文仅仅简单介绍了spock的使用,而spock提供了大量能使我们快速便捷地处理数据的函数和方法,经典测试的案例,敬请期待!。
边栏推荐
- 力扣5_876. 链表的中间结点
- 很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
- 【每日一题】736. Lisp 语法解析
- For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
- leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
- JVM memory and garbage collection-3-object instantiation and memory layout
- Ml self realization / logistic regression / binary classification
- 文盘Rust -- 给程序加个日志
- 云原生应用开发之 gRPC 入门
- Installing and using mpi4py
猜你喜欢
Random walk reasoning and learning in large-scale knowledge base
MQTT X Newsletter 2022-06 | v1.8.0 发布,新增 MQTT CLI 和 MQTT WebSocket 工具
metasploit
metasploit
[reinforcement learning medical] deep reinforcement learning for clinical decision support: a brief overview
Ml self realization /knn/ classification / weightlessness
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
[knowledge map paper] Devine: a generative anti imitation learning framework for knowledge map reasoning
Introduction to ADB tools
CorelDRAW2022下载安装电脑系统要求技术规格
随机推荐
nmap工具介绍及常用命令
Ml self realization / logistic regression / binary classification
阿锅鱼的大度
Keras' deep learning practice -- gender classification based on inception V3
WPF custom realistic wind radar chart control
leetcode 866. Prime Palindrome | 866. 回文素数
JVM memory and garbage collection-3-runtime data area / method area
Emqx 5.0 release: open source Internet of things message server with single cluster supporting 100million mqtt connections
谈谈 SAP 系统的权限管控和事务记录功能的实现
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
If time is a river
Reading notes of Clickhouse principle analysis and Application Practice (7)
常见的磁盘格式以及它们之间的区别
burpsuite
Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
金融业数字化转型中,业务和技术融合需要经历三个阶段
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
Monthly observation of internet medical field in May 2022
Alo who likes TestMan