当前位置:网站首页>Spock单元测试框架介绍及在美团优选的实践_第二章(static静态方法mock方式)
Spock单元测试框架介绍及在美团优选的实践_第二章(static静态方法mock方式)
2022-07-08 00:53: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订单业务类进行单测
说明:
UserUtils.getUser()是个静态方法
3.创建Groovy.class类
new—>Groovy Class类
4.创建GoodsServiceSpec.java
创建GoodsServiceSpec类对,queryGoods()方法进行单元测试,命名严格要求:要测的类名+Spec
说明:
1、创建类之后需要继承extends Specification类
3、mock静态方法(ststic) 需要 setup()函数进行处理,返回需要的值即可。
4、创建测试方法格式如下
/** * 灰太狼永远吃不到羊 */
class ceshiSpec extends Specification{
def goodsService = new GoodsService()
/** * setup() 函数的意思是在测试类启动加载之前初始化静态资源 * @return */
def setup() {
new MockUp<UserUtils>() {
@Mock
static User getUser() {
return new User(login: "小新新")
}
}
}
def "queryGoods success"() {
given:"测试mock,UserUtils的getUser()静态方法"
and:"mock方法"
//when 标签代表要测试方法的入口,只能又一个方法
when:"测试方法"
def response = goodsService.queryGoods()
//then 对结果进行验证
then:"校验结果"
response.get(0) == "小新新"
}
}
实际效果与预期效果一致,表示测试通过。
实际效果与预期效果不一致,表示测试失败
5.groowy简单语法
where以标准格式开使提供集合
when调用指定方法函数
then: 故出断高裝达式
expect 期站的行为,shen-then的特点
giveni mock单测中指定mock数信
throwin:如果在when方法中地出现异常,则在这个子句中会捕玩到异常并返回
def setup{}:每个训过运行前的启动方法
der cleanup{}:每个加武运行居的清理万法
def setupSpec{}:每个测试前的启动方法
def cleanupSpec{}:服后一介测试运行后的消理方法
总结
以上就是今天要讲的内容,本文仅仅简单介绍了spock的使用,而spock提供了大量能使我们快速便捷地处理数据的函数和方法,后续我将Spock对于异常、void方法场景经典测试的案例,敬请期待!。
边栏推荐
- Can you write the software test questions?
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- Relationship between bizdevops and Devops
- Key points of data link layer and network layer protocol
- leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
- 生命的高度
- 【每日一题】648. 单词替换
- [knowledge map paper] attnpath: integrate the graph attention mechanism into knowledge graph reasoning based on deep reinforcement
- 微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
- Talk about the cloud deployment of local projects created by SAP IRPA studio
猜你喜欢
adb工具介绍
Thread deadlock -- conditions for deadlock generation
Kwai applet guaranteed payment PHP source code packaging
leetcode 866. Prime Palindrome | 866. prime palindromes
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)
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
《ClickHouse原理解析与应用实践》读书笔记(7)
Leetcode featured 200 channels -- array article
Common disk formats and the differences between them
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
随机推荐
Relationship between bizdevops and Devops
Neural network and deep learning-5-perceptron-pytorch
Where to think
BizDevOps与DevOps的关系
关于TXE和TC标志位的小知识
Monthly observation of internet medical field in May 2022
Leetcode question brushing record | 27_ Removing Elements
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
C language -cmake cmakelists Txt tutorial
XXL job of distributed timed tasks
Keras深度学习实战——基于Inception v3实现性别分类
VIM string substitution
【每日一题】648. 单词替换
Key points of data link layer and network layer protocol
Disk rust -- add a log to the program
LeetCode精选200道--数组篇
Leetcode question brushing record | 485_ Maximum number of consecutive ones
Emqx 5.0 release: open source Internet of things message server with single cluster supporting 100million mqtt connections
阿锅鱼的大度
Redismission source code analysis