Spock单元测试框架简介及实践

一、前言

单元测试(Unit Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。在过程化编程中,一个单元就是单个程序、函数、过程等;对于面向对象编程,最小单元就是方法,包括基类(超类)、抽象类、或者派生类(子类)中的方法。 —— 维基百科

为什么要写单元测试?

to be or not to be?先说一下为什么会排斥写单元测试,这一点大多数开发同学都会有相同的感受:

  1. 项目本身单元测试不完整或缺失,需要花费大量精力去填补;
  2. 任务重工期紧,代码都没时间写,更别提单测了;
  3. 单元测试用例编写繁琐,构造入参、mock 方法,其代码量往往比业务改动的代码还要多;
  4. 认为单元测试无用,写起来浪费时间;
  5. Java语法本身就很啰嗦了,还要去写啰嗦的测试用例;
  6. ...

那为什么又要写单元测试 ?

  1. 单元测试在软件开发过程的早期就能发现问题;
  2. 单元测试可以延续用于准确反映当任何变更发生时可执行程序和代码的表现;
  3. 单元测试能一定程度上消除程序单元的不可靠,采用自底向上的测试路径。先测试程序部件再测试部件组装,使集成测试变得更加简单。
  4. 单元测试提供了系统化的一种文档记录,开发人员可以直观的理解程序单元的基础 API。
  5. ...
为什么要使用Spock?

或者说为什么不用 Junit 或其他传统的单测框架?我们可以先看几张图(图左 Junit,图右 Spock)

我们能清晰的看到,借助于 Spock 框架以及 Groovy 语言强大的语法,我们能很轻易的构造测试数据、Mock 接口返回行为。通过 Spock的数据驱动测试( Data Driven Testing )可以快速、清晰的汇聚大量测试用例用于覆盖复杂场景的各个分支。

单元测试中的 Java & Groovy

如果感觉图例不够清晰,我们可以简单看下使用 Groovy 与 Java 编写单测的区别:

  1. // Groovy:创建对象并初始化

  2. def param = new XXXApprovalParam(id: 123, taskId: "456")

  3. // Java:创建对象并初始化

  4. XXXApprovalParam param1 = new XXXApprovalParam();

  5. param1.setId(123L);

  6. param1.setTaskId("456");

  7. // Groovy:创建集合

  8. def list = [param]

  9. // Java:创建集合

  10. List<XXXApprovalParam> list1 = new ArrayList<>()

  11. list1.add(param1)

  12. // Groovy:创建空Map

  13. def map = [:]

  14. // Java:创建空Map

  15. Map<String, Object> map1 = new HashMap<>()

  16. // Groovy:创建非空Map

  17. def map = ["key":"value"]

  18. // Java:创建非空Map

  19. Map<String, String> map1 = new HashMap<>()

  20. map1.put("key", "value")

  21. // 实践:Mock方法返回一个复杂对象

  22. {

  23.    "result":{

  24.        "data":[

  25.           {

  26.                "fullName":"张三",

  27.                "id":123

  28.           }

  29.       ],

  30.        "empty":false,

  31.        "total":1

  32.   },

  33.    "success":true

  34. }

  35. xxxReadService.getSomething(*_) >> Response.ok(new Paging(1L, [new Something(id: 123, fullName: "张三")]))

  36. |             |             |      |

  37. |             |             |      生成返回值

  38. |             |             匹配任意个参数(单个参数可以使用:_)

  39. |             方法

  40. 对象

看到这里你可能会对 Spock 产生了一点点兴趣,那我们进入下一章,从最基础的概念开始入手。

二、基本概念

Specification

 
  1. class MyFirstSpecification extends Specification {

  2.    // fields

  3.    // fixture methods

  4.    // feature methods

  5.    // helper methods

  6. }

Sopck 单元测试类都需要去继承 Specification,为我们提供了诸如 Mock、Stub、with、verifyAll 等特性。

Fields
  1. def obj = new ClassUnderSpecification()

  2. def coll = new Collaborator()

  3. @Shared

  4. def res = new VeryExpensiveResource()

实例字段是存储 Specification 固有对象(fixture objects)的好地方,最好在声明时初始化它们。存储在实例字段中的对象不会在测试方法之间共享。相反,每个测试方法都应该有自己的对象,这有助于特征方法之间的相互隔离。这通常是一个理想的目标,如果想在不同的测试方法之间共享对象,可以通过声明 @Shared 注解实现。

Fixture Methods
 
  1. def setupSpec() {}    // runs once - before the first feature method

  2. def setup() {}        // runs before every feature method

  3. def cleanup() {}      // runs after every feature method

  4. def cleanupSpec() {}  // runs once - after the last feature method

固有方法(我们暂定这么称呼 ta)负责设置和清理运行(特征方法)环境。建议使用 setup()、cleanup()为每个特征方法(feature method)设置新的固有对象(fixture objects),当然这些固有方法是可选的。 Fixture Method 调用顺序:

  1. super.setupSpec
  2. sub.setupSpec
  3. super.setup
  4. sub.setup
  5. feature method *
  6. sub.cleanup
  7. super.cleanup
  8. sub.cleanupSpec
  9. super.cleanupSpec
Feature Methods
  1. def "echo test"() {

  2. // blocks go here

  3. }

特征方法即我们需要写的单元测试方法,Spock 为特征方法的各个阶段提供了一些内置支持——即特征方法的 block。Spock 针对特征方法提供了六种 block:given、when、then、expect、cleanup和where。

given

 
  1. given:

  2. def stack = new Stack()

  3. def elem = "push me"

  4. // Demo

  5. def "message send test"() {

  6.    given:

  7.    def param = xxx;

  8.    userService.getUser(*_) >> Response.ok(new User())

  9.   ...

  10. }

given block 功能类似 setup block,在特征方法执行前的前置准备工作,例如构造一些通用对象,mock 方法的返回。given block 默认可以省略,即特征方法开头和第一个显式块之间的任何语句都属于隐式 given 块。

when-then
 
  1. when:   // stimulus

  2. then:   // response

  3. // Demo

  4. def "message send test"() {

  5.    given:

  6.    def param = xxx;

  7.    userService.getUser(*_) >> Response.ok(new User())

  8.    when:

  9.    def response = messageService.snedMessage(param)

  10.    then:

  11.    response.success

  12. }

when-then block 描述了单元测试过程中通过输入 command 获取预期的 response,when block 可以包含任意代码(例如参数构造,接口行为mock),但 then block 仅限于条件、交互和变量定义,一个特征方法可以包含多对 when-then block。 如果断言失败会发生什么呢?如下所示,Spock 会捕获评估条件期间产生的变量,并以易于理解的形式呈现它们:、

 
  1. Condition not satisfied:

  2. result.success

  3. | |

  4. | false

  5. Response{success=false, error=}

异常条件
 
  1. def "message send test"() {

  2. when:

  3. def response = messageService.snedMessage(null)

  4. then:

  5. def error = thrown(BizException)

  6. error.code == -1

  7. }

  8. // 同上

  9. def "message send test"() {

  10. when:

  11. def response = messageService.snedMessage(null)

  12. then:

  13. BizException error = thrown()

  14. error.code == -1

  15. }

  16. // 不应该抛出xxx异常

  17. def "message send test"() {

  18. when:

  19. def response = messageService.snedMessage(null)

  20. then:

  21. notThrown(NullPointerException)

  22. }

Interactions

这里使用官网的一个例子,描述的是当发布者发送消息后,两个订阅者都只收到一次该消息,基于交互的测试方法将在后续单独的章节中详细介绍。

 
  1. def "events are published to all subscribers"() {

  2. given:

  3. def subscriber1 = Mock(Subscriber)

  4. def subscriber2 = Mock(Subscriber)

  5. def publisher = new Publisher()

  6. publisher.add(subscriber1)

  7. publisher.add(subscriber2)

  8. when:

  9. publisher.fire("event")

  10. then:

  11. 1 * subscriber1.receive("event")

  12. 1 * subscriber2.receive("event")

  13. }

expect

expect block 是 when-then block 的一种简化用法,一般 when-then block 描述具有副作用的方法,expect block 描述纯函数的方法(不具有副作用)。

 
  1. // when-then block

  2. when:

  3. def x = Math.max(1, 2)

  4. then:

  5. x == 2

  6. // expect block

  7. expect:

  8. Math.max(1, 2) == 2

cleanup

用于释放资源、清理文件系统、管理数据库连接或关闭网络服务。

 
  1. given:

  2. def file = new File("/some/path")

  3. file.createNewFile()

  4. // ...

  5. cleanup:

  6. file.delete()

where

where block 用于编写数据驱动的特征方法,如下 demo 创建了两组测试用例,第一组a=5,b=1,c=5,第二组:a=3,b=9,c=9,关于 where 的详细用法会在后续的数据驱动章节进一步介绍。

 
  1. def "computing the maximum of two numbers"() {

  2. expect:

  3. Math.max(a, b) == c

  4. where:

  5. a << [5, 3]

  6. b << [1, 9]

  7. c << [5, 9]

  8. }

Helper Methods

当特征方法包含大量重复代码的时候,引入一个或多个辅助方法是很有必要的。例如设置/清理逻辑或复杂条件,但是不建议过分依赖,这会导致不同的特征方法之间过分耦合(当然 fixture methods 也存在该问题)。 这里引入官网的一个案例:

 
  1. def "offered PC matches preferred configuration"() {

  2. when:

  3. def pc = shop.buyPc()

  4. then:

  5. pc.vendor == "Sunny"

  6. pc.clockRate >= 2333

  7. pc.ram >= 4096

  8. pc.os == "Linux"

  9. }

  10. // 引入辅助方法简化条件判断

  11. def "offered PC matches preferred configuration"() {

  12. when:

  13. def pc = shop.buyPc()

  14. then:

  15. matchesPreferredConfiguration(pc)

  16. }

  17. def matchesPreferredConfiguration(pc) {

  18. pc.vendor == "Sunny"

  19. && pc.clockRate >= 2333

  20. && pc.ram >= 4096

  21. && pc.os == "Linux"

  22. }

  23. // exception

  24. // Condition not satisfied:

  25. //

  26. // matchesPreferredConfiguration(pc)

  27. // | |

  28. // false ...

上述方法在发生异常时 Spock 给以的提示不是很有帮助,所以我们可以做些调整:

 
  1. void matchesPreferredConfiguration(pc) {

  2. assert pc.vendor == "Sunny"

  3. assert pc.clockRate >= 2333

  4. assert pc.ram >= 4096

  5. assert pc.os == "Linux"

  6. }

  7. // Condition not satisfied:

  8. //

  9. // assert pc.clockRate >= 2333

  10. // | | |

  11. // | 1666 false

  12. // ...

with

with 是 Specification 内置的一个方法,有点 ES6 对象解构的味道了。当然,作为辅助方法的替代方法,在多条件判断的时候非常有用:

 
  1. def "offered PC matches preferred configuration"() {

  2. when:

  3. def pc = shop.buyPc()

  4. then:

  5. with(pc) {

  6. vendor == "Sunny"

  7. clockRate >= 2333

  8. ram >= 406

  9. os == "Linux"

  10. }

  11. }

  12. def "service test"() {

  13. def service = Mock(Service) // has start(), stop(), and doWork() methods

  14. def app = new Application(service) // controls the lifecycle of the service

  15. when:

  16. app.run()

  17. then:

  18. with(service) {

  19. 1 * start()

  20. 1 * doWork()

  21. 1 * stop()

  22. }

  23. }

verifyAll

在多条件判断的时候,通常在遇到失败的断言后,就不会执行后续判断(类似短路与)。我们可以借助 verifyAll 在测试失败前收集所有的失败信息,这种行为也称为软断言:

 
  1. def "offered PC matches preferred configuration"() {

  2. when:

  3. def pc = shop.buyPc()

  4. then:

  5. verifyAll(pc) {

  6. vendor == "Sunny"

  7. clockRate >= 2333

  8. ram >= 406

  9. os == "Linux"

  10. }

  11. }

  12. // 也可以在没有目标的情况下使用

  13. expect:

  14. verifyAll {

  15. 2 == 2

  16. 4 == 4

  17. }

Specifications as Documentation

Spock 允许我们在每个 block 后面增加双引号添加描述,在不改变方法语意的前提下来提供更多的有价值信息(非强制)。

 
  1. def "offered PC matches preferred configuration"() {

  2. when: "购买电脑"

  3. def pc = shop.buyPc()

  4. then: "验证结果"

  5. with(pc) {

  6. vendor == "Sunny"

  7. clockRate >= 2333

  8. ram >= 406

  9. os == "Linux"

  10. }

  11. }

Comparison to Junit

SpockJUnit
SpecificationTest class
setup()@Before
cleanup()@After
setupSpec()@BeforeClass
cleanupSpec()@AfterClass
FeatureTest
Feature methodTest method
Data-driven featureTheory
ConditionAssertion
Exception condition@Test(expected=…)
InteractionMock expectation (e.g. in Mockito)

三、数据驱动

Spock 数据驱动测试(Data Driven Testing),可以很清晰地汇集大量测试数据:

数据表(Data Table)

表的第一行称为表头,用于声明数据变量。随后的行称为表行,包含相应的值。每一行 特征方法将会执行一次,我们称之为方法的一次迭代。如果一次迭代失败,剩余的迭代仍然会被执行,特征方法执行结束后将会报告所有故障。 如果需要在迭代之间共享一个对象,例如 applicationContext,需要将其保存在一个 @Shared 或静态字段中。例如 @Shared applicationContext = xxx 。 数据表必须至少有两列,一个单列表可以写成:

 
  1. where:

  2. destDistrict | _

  3. null | _

  4. "339900" | _

  5. null | _

  6. "339900" | _

输入和预期输出可以用双管道符号 ( || ) 分割,以便在视觉上将他们分开:

 
  1. where:

  2. destDistrict || _

  3. null || _

  4. "339900" || _

  5. null || _

  6. "339900" || _

可以通过在方法上标注 @Unroll 快速展开数据表的测试用例,还可以通过占位符动态化方法名: 

数据管道(Data Pipes)

数据表不是为数据变量提供值的唯一方法,实际上数据表只是一个或多个数据管道的语法糖:

 
  1. ...

  2. where:

  3. destDistrict << [null, "339900", null, "339900"]

  4. currentLoginUser << [null, null, loginUser, loginUser]

  5. result << [false, false, false, true]

由左移 ( << ) 运算符指示的数据管道将数据变量连接到数据提供者。数据提供者保存变量的所有值,每次迭代一个。任何可遍历的对象都可以用作数据提供者。包括 Collection、String、Iterable 及其子类。 数据提供者不一定是数据,他们可以从文本文件、数据库和电子表格等外部源获取数据,或者随机生成数据。仅在需要时(在下一次迭代之前)查询下一个值。

多变量的数据管道(Multi-Variable Data Pipes Data Table)
 
  1. @Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver")

  2. def "maximum of two numbers"() {

  3. expect:

  4. Math.max(a, b) == c

  5. where:

  6. [a, b, c] << sql.rows("select a, b, c from maxdata")

  7. }

可以用下划线( _ )忽略不感兴趣的数据值:

  1. ...

  2. where:

  3. [a, b, _, c] << sql.rows("select * from maxdata")

实际对 DAO 层进行测试时,一般会通过引入内存数据库(如h2)进行数据库隔离,避免数据之间相互干扰。这里平时使用不多,就不过多介绍,感兴趣的移步官方文档 → 传送门

四、基于交互的测试

属性Mock
  1. interface Subscriber {

  2. String receive(String message)

  3. }

如果我们想每次调用 Subscriber#receive 的时候返回“ok”,使用 Spock 的写法会简洁直观很多:

  1. // Mockito

  2. when(subscriber.receive(any())).thenReturn("ok");

  3. // Spock

  4. subscriber.receive(_) >> "ok"

测试桩
  1. subscriber.receive(_) >> "ok"

  2. | | | |

  3. | | | 生成返回值

  4. | | 匹配任意参数(多个参数可以使用:*_)

  5. | 方法

  6. 对象

_ 类似 Mockito 的 any(),如果有同名的方法,可以使用 as 进行参数类型区分

subscriber.receive(_ as String) >> "ok"
固定返回值

我们已经看到了使用右移 ( >> ) 运算符返回一个固定值,如果想根据不同的调用返回不同的值,可以:

  1. subscriber.receive("message1") >> "ok"

  2. subscriber.receive("message2") >> "fail"

序列返回值

如果想在连续调用中返回不用的值,可以使用三重右移运算符(>>>):

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

该特性在写批处理方法单元测试用例的时候尤为好用,我们可以在指定的循环次数当中返回 null 或者空的集合,来中断流程,例如

 
  1. // Spock

  2. businessDAO.selectByQuery(_) >>> [[new XXXBusinessDO()], null]

  3. // 业务代码

  4. DataQuery dataQuery = new DataQuery();

  5. dataQuery.setPageSize(100);

  6. Integer pageNo = 1;

  7. while (true) {

  8. dataQuery.setPageNo(pageNo);

  9. List<XXXBusinessDO> xxxBusinessDO = businessDAO.selectByQuery(dataQuery);

  10. if (CollectionUtils.isEmpty(xxxBusinessDO)) {

  11. break;

  12. }

  13. dataHandle(xxxBusinessDO);

  14. pageNo++;

  15. }

可计算返回值

如果想根据方法的参数计算出返回值,请将右移 ( >> ) 运算符与闭包一起使用:

subscriber.receive(_) >> { args -> args[0].size() > 3 ? "ok" : "fail" }
异常返回值

有时候你想做的不仅仅是计算返回值,例如抛一个异常:

subscriber.receive(_) >> { throw new InternalError("ouch") }
链式返回值
subscriber.receive(_) >>> ["ok", "fail", "ok"] >> { throw new InternalError() } >> "ok"

前三次调用分别返回"ok", "fail", "ok",第四次调用会抛出 InternalError 异常,之后的调用都会返回 “ok”

默认返回值

有时候并不关心返回的内容,只需要其不为 null 即可,下述代码的结果和 Stub() 创建的代理对象调用效果一致:

subscriber.receive(_) >> _
Spy

Spy 和 Mock、Stub 有些区别,一般不太建议使用该功能,但是这里还是会简单补充介绍下。 Spy 必须基于真实的对象(Mock、Stub 可以基于接口),通过 Spy 的名字可以很明显猜到 ta 的用途——对于 Spy 对象的方法调用会自动委托给真实对象,然后从真实对象的方法返回值会通过 Spy 传递回调用者。 但是如果给 Spy 对象设置测试桩,将不会调用真正的方法:

subscriber.receive(_) >> "ok"

通过Spy也可以实现部分Mock:

 
  1. // this is now the object under specification, not a collaborator

  2. MessagePersister persister = Spy {

  3. // stub a call on the same object

  4. isPersistable(_) >> true

  5. }

交互约束

我们可以通过交互约束去校验方法被调的次数

 
  1. def "should send messages to all subscribers"() {

  2. when:

  3. publisher.send("hello")

  4. then:

  5. 1 * subscriber.receive("hello")

  6. 1 * subscriber2.receive("hello")

  7. }

  8. // 说明:当发布者发送消息时,两个订阅者都应该只收到一次消息

  9. 1 * subscriber.receive("hello")

  10. | | | |

  11. | | | 参数约束

  12. | | 方法约束

  13. | 目标约束

  14. 基数(方法执行次数)

Spock 扩展 → 传送门 Spock Spring 模块 → 传送门

五、进阶玩法

单元测试代码生成插件

Spock框架凭借其优秀的设计以及借助 Groovy 脚本语言的便捷性,在一众单元测试框架中脱颖而出。但是写单元测试还是需要一定的时间,那有没有办法降低写单元测试的成本呢? 通过观察一个单元测试类的结构,大致分为创建目标测试类、创建目标测试类 Mock 属性、依赖注入、还有多个特征方法,包括特征方法中的 when-then block,都是可以通过扫描目标测试类获取类的结构信息后自动生成。

当然我们不用重复造轮子,通过 IDEA TestMe 插件,可以轻松完成上述任务,TestMe 默认支持以下单元测试框架:

TestMe已经支持 Groovy 和 Spock,操作方法:选中需要生成单元测试类的目标类 → 右键 Generate... → TestMe → Parameterized Groovy, Spock & Mockito 。

但是默认模版生成的生成的单元测试代码使用 的是 Spock & Mockito 混合使用,没有使用 Spock 的测试桩等特性。不过 TestMe 提供了自定义单元测试类生成模版的能力,我们可以实现如下效果:

 
  1. // 默认模版

  2. class UserServiceTest extends Specification {

  3. @Mock

  4. UserDao userDao

  5. @InjectMocks

  6. UserService userService

  7. def setup() {

  8. MockitoAnnotations.initMocks(this)

  9. }

  10. @Unroll

  11. def "find User where userQuery=#userQuery then expect: #expectedResult"() {

  12. given:

  13. when(userDao.findUserById(anyLong(), anyBoolean())).thenReturn(new UserDto())

  14. when:

  15. UserDto result = userService.findUser(new UserQuery())

  16. then:

  17. result == new UserDto()

  18. }

  19. }

 
  1. // 修改后的模版

  2. class UserServiceGroovyTest extends Specification {

  3. def userService = new UserService()

  4. def userDao = Mock(UserDao)

  5. def setup() {

  6. userService.userDao = userDao

  7. }

  8. @Unroll

  9. def "findUserTest includeDeleted->#includeDeleted"() {

  10. given:

  11. userDao.findUserById(*_) >> new UserDto()

  12. when:

  13. UserDto result = userService.findUser(new UserQuery())

  14. then:

  15. result == new UserDto()

  16. }

  17. }

修改后的模版主要是移除 mockito 的依赖,避免两种框架混合使用降低了代码的简洁和可读性。当然代码生成完我们还需要对单元测试用例进行一些调整,例如入参属性设置、测试桩行为设置等等。

新增模版的操作也很简单,IDEA → Preference... → TestMe → TestMe Templates Test Class

 
  1. #parse("TestMe macros.groovy")

  2. #parse("Zcy macros.groovy")

  3. #if($PACKAGE_NAME)

  4. package ${PACKAGE_NAME}

  5. #end

  6. import spock.lang.*

  7. #parse("File Header.java")

  8. class ${CLASS_NAME} extends Specification {

  9. #grRenderTestInit4Spock($TESTED_CLASS)

  10. #grRenderMockedFields4Spock($TESTED_CLASS.fields)

  11. def setup() {

  12. #grSetupMockedFields4Spock($TESTED_CLASS)

  13. }

  14. #foreach($method in $TESTED_CLASS.methods)

  15. #if($TestSubjectUtils.shouldBeTested($method))

  16. #set($paraTestComponents=$TestBuilder.buildPrameterizedTestComponents($method,$grReplacementTypesForReturn,$grReplacementTypes,$grDefaultTypeValues))

  17. def "$method.name$testSuffix"() {

  18. #if($MockitoMockBuilder.shouldStub($method,$TESTED_CLASS.fields))

  19. given:

  20. #grRenderMockStubs4Spock($method,$TESTED_CLASS.fields)

  21. #end

  22. when:

  23. #grRenderMethodCall($method,$TESTED_CLASS.name)

  24. then:

  25. #if($method.hasReturn())

  26. #grRenderAssert($method)

  27. #{else}

  28. noExceptionThrown() // todo - validate something

  29. #end

  30. }

  31. #end

  32. #end

  33. }

Includes

 
  1. #parse("TestMe common macros.java")

  2. ################## Global vars ###############

  3. #set($grReplacementTypesStatic = {

  4. "java.util.Collection": "[<VAL>]",

  5. "java.util.Deque": "new LinkedList([<VAL>])",

  6. "java.util.List": "[<VAL>]",

  7. "java.util.Map": "[<VAL>:<VAL>]",

  8. "java.util.NavigableMap": "new java.util.TreeMap([<VAL>:<VAL>])",

  9. "java.util.NavigableSet": "new java.util.TreeSet([<VAL>])",

  10. "java.util.Queue": "new java.util.LinkedList<TYPES>([<VAL>])",

  11. "java.util.RandomAccess": "new java.util.Vector([<VAL>])",

  12. "java.util.Set": "[<VAL>] as java.util.Set<TYPES>",

  13. "java.util.SortedSet": "[<VAL>] as java.util.SortedSet<TYPES>",

  14. "java.util.LinkedList": "new java.util.LinkedList<TYPES>([<VAL>])",

  15. "java.util.ArrayList": "[<VAL>]",

  16. "java.util.HashMap": "[<VAL>:<VAL>]",

  17. "java.util.TreeMap": "new java.util.TreeMap<TYPES>([<VAL>:<VAL>])",

  18. "java.util.LinkedList": "new java.util.LinkedList<TYPES>([<VAL>])",

  19. "java.util.Vector": "new java.util.Vector([<VAL>])",

  20. "java.util.HashSet": "[<VAL>] as java.util.HashSet",

  21. "java.util.Stack": "new java.util.Stack<TYPES>(){{push(<VAL>)}}",

  22. "java.util.LinkedHashMap": "[<VAL>:<VAL>]",

  23. "java.util.TreeSet": "[<VAL>] as java.util.TreeSet"

  24. })

  25. #set($grReplacementTypes = $grReplacementTypesStatic.clone())

  26. #set($grReplacementTypesForReturn = $grReplacementTypesStatic.clone())

  27. #set($testSuffix="Test")

  28. #foreach($javaFutureType in $TestSubjectUtils.javaFutureTypes)

  29. #evaluate(${grReplacementTypes.put($javaFutureType,"java.util.concurrent.CompletableFuture.completedFuture(<VAL>)")})

  30. #end

  31. #foreach($javaFutureType in $TestSubjectUtils.javaFutureTypes)

  32. #evaluate(${grReplacementTypesForReturn.put($javaFutureType,"<VAL>")})

  33. #end

  34. #set($grDefaultTypeValues = {

  35. "byte": "(byte)0",

  36. "short": "(short)0",

  37. "int": "0",

  38. "long": "0l",

  39. "float": "0f",

  40. "double": "0d",

  41. "char": "(char)'a'",

  42. "boolean": "true",

  43. "java.lang.Byte": """00110"" as Byte",

  44. "java.lang.Short": "(short)0",

  45. "java.lang.Integer": "0",

  46. "java.lang.Long": "1l",

  47. "java.lang.Float": "1.1f",

  48. "java.lang.Double": "0d",

  49. "java.lang.Character": "'a' as Character",

  50. "java.lang.Boolean": "Boolean.TRUE",

  51. "java.math.BigDecimal": "0 as java.math.BigDecimal",

  52. "java.math.BigInteger": "0g",

  53. "java.util.Date": "new java.util.GregorianCalendar($YEAR, java.util.Calendar.$MONTH_NAME_EN.toUpperCase(), $DAY_NUMERIC, $HOUR_NUMERIC, $MINUTE_NUMERIC).getTime()",

  54. "java.time.LocalDate": "java.time.LocalDate.of($YEAR, java.time.Month.$MONTH_NAME_EN.toUpperCase(), $DAY_NUMERIC)",

  55. "java.time.LocalDateTime": "java.time.LocalDateTime.of($YEAR, java.time.Month.$MONTH_NAME_EN.toUpperCase(), $DAY_NUMERIC, $HOUR_NUMERIC, $MINUTE_NUMERIC, $SECOND_NUMERIC)",

  56. "java.time.LocalTime": "java.time.LocalTime.of($HOUR_NUMERIC, $MINUTE_NUMERIC, $SECOND_NUMERIC)",

  57. "java.time.Instant": "java.time.LocalDateTime.of($YEAR, java.time.Month.$MONTH_NAME_EN.toUpperCase(), $DAY_NUMERIC, $HOUR_NUMERIC, $MINUTE_NUMERIC, $SECOND_NUMERIC).toInstant(java.time.ZoneOffset.UTC)",

  58. "java.io.File": "new File(getClass().getResource(""/$PACKAGE_NAME.replace('.','/')/PleaseReplaceMeWithTestFile.txt"").getFile())",

  59. "java.lang.Class": "Class.forName(""$TESTED_CLASS.canonicalName"")"

  60. })

  61. ##

  62. ##

  63. ################## Macros #####################

  64. ####

  65. ################## Custom Macros #####################

  66. #macro(grRenderMockStubs4Spock $method $testedClassFields)

  67. #foreach($field in $testedClassFields)

  68. #if($MockitoMockBuilder.isMockable($field))

  69. #foreach($fieldMethod in $field.type.methods)

  70. #if($fieldMethod.returnType && $fieldMethod.returnType.name !="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method))

  71. #if($fieldMethod.returnType.name == "T" || $fieldMethod.returnType.canonicalName.indexOf("<T>") != -1)

  72. $field.name.${fieldMethod.name}(*_) >> null

  73. #else

  74. $field.name.${fieldMethod.name}(*_) >> $TestBuilder.renderReturnParam($method,$fieldMethod.returnType,"${fieldMethod.name}Response",$grReplacementTypes,$grDefaultTypeValues)

  75. #end

  76. #end

  77. #end

  78. #end

  79. #end

  80. #end

  81. ##

  82. #macro(grRenderMockedFields4Spock $testedClassFields)

  83. #foreach($field in $testedClassFields)

  84. #if($field.name != "log")

  85. #if(${field.name.indexOf("PoolExecutor")}!=-1)

  86. def $field.name = Executors.newFixedThreadPool(2)

  87. #else

  88. def $field.name = Mock($field.type.canonicalName)

  89. #end

  90. #end

  91. #end

  92. #end

  93. ##

  94. #macro(grRenderTestInit4Spock $testedClass)

  95. def $StringUtils.deCapitalizeFirstLetter($testedClass.name) = $TestBuilder.renderInitType($testedClass,"$testedClass.name",$grReplacementTypes,$grDefaultTypeValues)

  96. #end

  97. ##

  98. #macro(grSetupMockedFields4Spock $testedClass)

  99. #foreach($field in $TESTED_CLASS.fields)

  100. #if($field.name != "log")

  101. $StringUtils.deCapitalizeFirstLetter($testedClass.name).$field.name = $field.name

  102. #end

  103. #end

  104. #end

最终效果如下:

当我们需要生成单元测试类的时候,可以选中需要生成单元测试类的目标类 → 右键 Generate... → TestMe → Spock for xxx。 当然,模版还在持续优化中,这里只是提供了一种解决方案,大家完全可以根据自己的实际需求进行调整。

六、补充说明

Spock依赖兼容

引入Spock的同时也需要引入 Groovy 的依赖,由于 Spock 使用指定 Groovy 版本进行编译和测试,很容易出现不兼容的情况。

  1. <groovy.version>3.0.12</groovy.version>

  2. <spock-spring.version>2.2-groovy-3.0</spock-spring.version>

  3. <dependency>

  4. <groupId>org.codehaus.groovy</groupId>

  5. <artifactId>groovy</artifactId>

  6. <version>${groovy.version}</version>

  7. <scope>test</scope>

  8. </dependency>

  9. <dependency>

  10. <groupId>org.spockframework</groupId>

  11. <artifactId>spock-spring</artifactId>

  12. <version>${spock-spring.version}</version>

  13. <scope>test</scope>

  14. </dependency>

  15. <plugin>

  16. <groupId>org.codehaus.gmavenplus</groupId>

  17. <artifactId>gmavenplus-plugin</artifactId>

  18. <version>1.13.1</version>

  19. <executions>

  20. <execution>

  21. <goals>

  22. <goal>compile</goal>

  23. <goal>compileTests</goal>

  24. </goals>

  25. </execution>

  26. </executions>

  27. </plugin>

这里提供一个版本选择的技巧,上面使用 spock-spring 的版本为 2.2-groovy-3.0,这里暗含了 groovy 的大版本为 3.0,通过 Maven Repo 也能看到,每次版本发布,针对 Groovy 的三个大版本都会提供相应的 Spock 版本以供选择。

 

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

          视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值