本文基于SpringBoot

在pom.xml添加Spock依赖

        <!-- test -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.1-groovy-2.4</version>
<scope>test</scope>
</dependency>

新建Sum.java

 public class Sum {
public int sum(int a, int b) {
return a + b;
}
}

新建groovy测试脚本SpockSpecification.groovy

 package com.test.bookup

 import com.example.demo.Sum
import spock.lang.Specification
import spock.lang.Unroll; /**
* Created by hande on 2018/7/18.
*/
class SpockSpecification extends Specification{ // 调用外部类测试
def sum = new Sum();
def "get sum"(){
expect:
sum.sum(,) ==
} // Where Blocks 简单大小比较函数测试
def "get max num"(){
expect:
Math.max(a,b) == c where:
a|b|c
||
||
||
} def "get min num"(){
expect:
Math.min(a,b) == c where:
a|b|c
||
||
||
} // 上述例子实际会跑三次测试,相当于在for循环中执行三次测试,如果在方法前声明@Unroll,则会当成三个方法运行。
@Unroll
def "@Unroll test"(){
expect:
Math.min(a,b) == c where:
a|b|c
||
||
||
} // where block另外两种数据定义方法
def "where block data init method"(){
expect:
Math.max(a,b) == c where:
a|_
|_
|_
|_ b<<[,,] c=a>b?a:b
} // When and Then Blocks
def "When and Then Blocks"(){
setup:
def stack = new Stack();
def em = "push me"; when:
stack.push(em); then:
!stack.empty();
stack.size() == ;
stack.peek() == em;
} // mock应用
Publisher publisher = new Publisher()
Subscriber subscriber = Mock()
Subscriber subscriber2 = Mock() def setup() {
publisher.subscribers.add(subscriber)
publisher.subscribers.add(subscriber2)
} def"should send messages to all subscribers"(){
when:
publisher.send("hello") then:
*subscriber.receive("hello")
*subscriber2.receive("hello")
}
// 上面的例子里验证了:在publisher调用send时,两个subscriber都应该被调用一次receive(“hello”)。
109 }

表达式中的次数、对象、函数和参数部分说明

 * subscriber.receive("hello")      // exactly one call
* subscriber.receive("hello") // zero calls
(..) * subscriber.receive("hello") // between one and three calls (inclusive)
(.._) * subscriber.receive("hello") // at least one call
(_..) * subscriber.receive("hello") // at most three calls
_ * subscriber.receive("hello") // any number of calls, including zero
* subscriber.receive("hello") // an argument that is equal to the String "hello"
* subscriber.receive(!"hello") // an argument that is unequal to the String "hello"
* subscriber.receive() // the empty argument list (would never match in our example)
* subscriber.receive(_) // any single argument (including null)
* subscriber.receive(*_) // any argument list (including the empty argument list)
* subscriber.receive(!null) // any non-null argument
* subscriber.receive(_ as String) // any non-null argument that is-a String
* subscriber.receive({ it.size() > }) // an argument that satisfies the given predicate
// (here: message length is greater than 3)
* subscriber._(*_) // any method on subscriber, with any argument list
* subscriber._ // shortcut for and preferred over the above
* _._ // any method call on any mock object
* _ // shortcut for and preferred over the above

Stubbing

对mock对象定义函数的返回值可以用如下方法:

subscriber.receive(_)>>"ok"

符号代表函数的返回值,执行上面的代码后,再调用subscriber.receice方法将返回ok。如果要每次调用返回不同结果,可以使用:

subscriber.receive(_) >>> ["ok", "error", "error", "ok"]

如果要做额外的操作,如抛出异常,可以使用:

subscriber.receive(_)>>{thrownewInternalError("ouch")}

而如果要每次调用都有不同的结果,可以把多次的返回连接起来:

subscriber.receive(_) >>> ["ok", "fail", "ok"] >> { throw new InternalError() } >> "ok"

mock and stubbing

如果既要判断某个mock对象的交互,又希望它返回值的话,可以结合mock和stub,可以这样:

then:
*subscriber.receive("message1")>>"ok"
*subscriber.receive("message2")>>"fail"

注意,spock不支持两次分别设定调用和返回值,如果把上例写成这样是错的:

setup:
subscriber.receive("message1")>>"ok" when:
publisher.send("message1") then:
*subscriber.receive("message1")

此时spock会对subscriber执行两次设定:

第一次设定receive(“message1”)只能调用一次,返回值为默认值(null)。
第二次设定receive(“message1”)会返回ok,不限制次数。

Spock集成入门的更多相关文章

  1. ①Jenkins集成—入门安装使用

    一.什么是Jenkins jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各 ...

  2. Jenkins持续集成 入门实践

    本文测试环境: ASP.NET MVC项目,Windows 7环境,SVN代码仓库, MSBuild,TortoiseSVN 持续集成这种工具很多了,Jenkins比较常用,他的原理就是一个服务,有一 ...

  3. ADF_Desktop Integration系列4_ADF桌面集成入门之部署ADF Desktop Excel

    2013-05-01 Created By BaoXinjian

  4. ADF_Desktop Integration系列3_ADF桌面集成入门之重定义ADF Desktop Excel

    2013-05-01 Created By BaoXinjian

  5. ADF_Desktop Integration系列2_ADF桌面集成入门之开发简单ADF Desktop Excel

    2013-05-01 Created By BaoXinjian

  6. ADF_Desktop Integration系列1_ADF桌面集成入门之设定Development Environment

    2013-05-01 Created By BaoXinjian

  7. 基于虹软人证核验 2.0 Android SDK开发集成入门

    一.功能介绍虹软人证核验 2.0 SDK(以下简称SDK)包含人脸检测.人脸跟踪.人证核验等能力,主要实现人证的1:1比对.其中暴露对外的功能方法有:active 引擎激活init 引擎初始化inpu ...

  8. 【ORM框架】Spring Data JPA(一)-- 入门

    本文参考:spring Data JPA入门   [原创]纯干货,Spring-data-jpa详解,全方位介绍  Spring Data JPA系列教程--入门 一.Spring Data JPA介 ...

  9. Web编译器Visual Studio扩展

    原文地址:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.WebCompiler 一个Visual Studio扩 ...

随机推荐

  1. iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)

    大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...

  2. 03-老马jQuery教程-DOM操作

    jQuery DOM操作 在没有jQuery之前,DOM的操作相对来说有点麻烦,尤其是DOM节点的搜索.目前我们已经学习了jQuery的选择器,接下带大家一块学习jQuery的DOM操作,jQuery ...

  3. .NET ramework 4.0安装失败

    1.停止Window 升级服务: 2.Windows 目录下的 SoftwareDistribution 重命名: 3.安装 .Net Framework 下面是百度上的方法,比较复杂 解决方法:1) ...

  4. egret list不显示问题

    1.循环滚动 2.单格滚动 不太清楚是把哪个约束勾上了,结果一直不显示,折腾了许久. 另外也不要把下面的这个约束勾上,否则不能滑动.

  5. 随手记Swift基础和Optional Type(问号?和感叹号!)

    距离Apple推出Swift已经有几天了,网上也时不时出现"急招Swift程序猿,要求有一天工作经验"的帖子. 看到Swift,除了苹果放的另外一门语言的链接(http://swi ...

  6. 【Unity】使用JSONObject解析Json

    为何要用JSONObject 之前已经用过JsonUtility和Newton.Json来解析Json了,为什么现在又要用一个新的JSONObject来解析Json? 使用JsonUtility:ht ...

  7. systemd管理网络应用

    采用systemd-networkd管理网卡 主网卡eth0的配置文件/etc/systemd/network/20-eth0.network,静态配置时内容示例如下: [Match] Name=et ...

  8. Jquery调用ajax参数说明

    代码中有详细注释,直接上代码. 注释掉的选项,一般用不到,直接用最基本的部分就可以了. $.ajax({ // 请求的URL url: '../Daily/Daily_Report', //HTTP ...

  9. linux 基础笔记(一)

    [1.1]系统的选择 Centos: CentOS(Community Enterprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise  ...

  10. [算法]从一道题引出variable-precision SWAR算法

    苏君君出了一道题,是牛客网上面的: 输入一个int型整数,输出该数二进制表示中1的个数.其中负数用补码表示. 其实这道题并不难,大家很容易想到的解法是转成字符串的思路,即如下所示: public st ...