Modules

Peter Niederwieser, The Spock Framework TeamVersion 1.1

Guice Module

Integration with the Guice IoC container. For examples see the specs in the codebase.

Spring Module

The Spring module enables integration with Spring TestContext Framework. It supports the following spring annotations @ContextConfiguration and @ContextHierarchy. Furthermore, it supports the meta-annotation @BootstrapWithand so any annotation that is annotated with @BootstrapWith will also work, such as @SpringBootTest@WebMvcTest.

Mocks

Spock 1.1 introduced the DetachedMockFactory and the SpockMockFactoryBean which allow the creation of Spock mocks outside of a specification.

NOTE

Although the mocks can be created outside of a specification, they only work inside the scope of a specification. So don’t perform any actions on them until they are attached to one.

Java Config

class DetachedJavaConfig {
def mockFactory = new DetachedMockFactory() @Bean
GreeterService serviceMock() {
return mockFactory.Mock(GreeterService)
} @Bean
GreeterService serviceStub() {
return mockFactory.Stub(GreeterService)
} @Bean
GreeterService serviceSpy() {
return mockFactory.Spy(GreeterServiceImpl)
} @Bean
FactoryBean<GreeterService> alternativeMock() {
return new SpockMockFactoryBean(GreeterService)
}
}

XML

Spock has spring namespace support, so if you declare the spock namespace with xmlns:spock="http://www.spockframework.org/spring" you get access to the convenience functions for creating mocks.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spock="http://www.spockframework.org/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.spockframework.org/spring http://www.spockframework.org/spring/spock.xsd"> <spock:mock id="serviceMock" class="org.spockframework.spring.docs.GreeterService"/> (1)
<spock:stub id="serviceStub" class="org.spockframework.spring.docs.GreeterService"/> (2)
<spock:spy id="serviceSpy" class="org.spockframework.spring.docs.GreeterServiceImpl"/> (3) <bean id="someExistingBean" class="java.util.ArrayList"/> (4)
<spock:wrapWithSpy ref="someExistingBean"/> (4) <bean id="alternativeMock" class="org.spockframework.spring.xml.SpockMockFactoryBean"> (5)
<constructor-arg value="org.spockframework.spring.docs.GreeterService"/>
<property name="mockNature" value="MOCK"/> (6)
</bean> </beans>
  1. Creates a Mock

  2. Creates a Stub

  3. Creates a Spy

  4. Wraps an existing bean with a Spy. Fails fast if referenced bean is not found.

  5. If you don’t want to use the special namespace support you can create the beans via the SpockMockFactoryBean

  6. The mockNature can be MOCKSTUB, or SPY and defaults to MOCK if not declared.

Usage

To use the mocks just inject them like any other bean and configure them as usual.

@Autowired @Named('serviceMock')
GreeterService serviceMock @Autowired @Named('serviceStub')
GreeterService serviceStub @Autowired @Named('serviceSpy')
GreeterService serviceSpy @Autowired @Named('alternativeMock')
GreeterService alternativeMock def "mock service"() {
when:
def result = serviceMock.greeting then:
result == 'mock me'
1 * serviceMock.getGreeting() >> 'mock me'
} def "sub service"() {
given:
serviceStub.getGreeting() >> 'stub me' expect:
serviceStub.greeting == 'stub me'
} def "spy service"() {
when:
def result = serviceSpy.greeting then:
result == 'Hello World'
1 * serviceSpy.getGreeting()
} def "alternatice mock service"() {
when:
def result = alternativeMock.greeting then:
result == 'mock me'
1 * alternativeMock.getGreeting() >> 'mock me'
}

Spring Boot

The recommended way to use Spock mocks in @WebMvcTest tests, is to use an embedded config annotated with @TestConfiguration and to create the mocks using the DetachedMockFactory.

@WebMvcTest
class WebMvcTestIntegrationSpec extends Specification { @Autowired
MockMvc mvc @Autowired
HelloWorldService helloWorldService def "spring context loads for web mvc slice"() {
given:
helloWorldService.getHelloMessage() >> 'hello world' expect: "controller is available"
mvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(status().isOk())
.andExpect(content().string("hello world"))
} @TestConfiguration
static class MockConfig {
def detachedMockFactory = new DetachedMockFactory() @Bean
HelloWorldService helloWorldService() {
return detachedMockFactory.Stub(HelloWorldService)
}
}
}

For more examples see the specs in the codebase and boot examples.

Scopes

Spock ignores bean that is not a singleton (in the singleton scope) by default. To enable mocks to work for scoped beans you need to add @ScanScopedBeans to the spec and make sure that the scope allows access to the bean during the setup phase.

NOTE
The request and session scope will throw exceptions by default, if there is no active request/session.

You can limit the scanning to certain scopes by using the value property of @ScanScopedBeans.

Tapestry Module

Integration with the Tapestry5 IoC container. For examples see the specs in the codebase.

Unitils Module

Integration with the Unitils library. For examples see the specs in the codebase.

Grails Module

The Grails plugin has moved to its own GitHub project.

NOTE
Grails 2.3 and higher have built-in Spock support and do not require a plugin.

Spock - Document -06 - Modules的更多相关文章

  1. Spock - Document -02 - Spock Primer

    Spock Primer Peter Niederwieser, The Spock Framework TeamVersion 1.1 This chapter assumes that you h ...

  2. Spock - Document -05 - Extensions

    Extensions Peter Niederwieser, The Spock Framework TeamVersion 1.1 Spock comes with a powerful exten ...

  3. Spock - Document -04- Interaction Based Testing

    Interaction Based Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Interaction-based ...

  4. Spock - Document - 03 - Data Driven Testing

    Data Driven Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Oftentimes, it is useful ...

  5. Spock - Document -01- introduction & Getting Started

    Introduction Peter Niederwieser, The Spock Framework TeamVersion 1.1 Spock is a testing and specific ...

  6. [Python] 06 - Modules --> Packages

    故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...

  7. Apache的初中级面试题

    --- 原文:[关于Apache的25个初中级面试题](http://www.oschina.net/translate/apache-interview-questions) Apache 求职面试 ...

  8. FILE SIGNATURES TABLE

    FILE SIGNATURES TABLE 16 December 2017 This table of file signatures (aka "magic numbers") ...

  9. [转]bigbluebutton中文社区 / 开放API / bbb API

    bigbluebutton中文社区 / 开放API / bbb API 创建会议 这个接口可以重复调用多次,而不会有副作用.这带来的好处就是能简化应用程序加会的流程,无论什么用户想要加会,都可以先创建 ...

随机推荐

  1. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

  2. [Oracle][DATAGUARD] 关于确认LOGICAL STANDBY的同期状况的方法

    Oracle的DATAGUARD环境,有PHYSICAL STANDBY和LOGICAL STANDBY两种.PHYSICAL STANDBY是传输REDO传到Standby端,然后由Standby端 ...

  3. Bouml快速使用指南

    一.Bouml简介 Android 系统中有大量Java.C++代码,继承以及依赖关系非常复杂,Bouml可以用c++.Java.Idl.Php和Python建模及生成代码,反之也可通过uml工具更好 ...

  4. redis应用-分布式锁

    一个操作要修改用户的状态,修改状态需要先读出用户的状态,在内存里进行修改,改完了再存回去.如果这样的操作同时进行了,就会出现并发问题,因为读取和保存状态这两个操作不是原子的. set lock:cod ...

  5. java算法01 - 链表

    1.链表 在Java中实现链表,每个节点都有一个值,然后把它链接到下一个节点.下面来看一下节点的实现 class Node<E> { private E e; private Node&l ...

  6. jenkins+svn完整打包并上传到linux服务器上

    因为公司用的是svn版本管理工具并且部署在了windows服务器上,所以测试环使用jenkins需要部署两套环境, 一套是在本地windows服务器,jenkins从svn下载代码完成打包并上传到li ...

  7. DevExpress v18.2新版亮点——DevExtreme篇(五)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExtreme Complete Sub ...

  8. 自己动手写CPU——寄存器堆、数据存储器(基于FPGA与Verilog)

    上一篇写的是基本的设计方案,由于考研复习很忙,不知道下一次什么时候才能打开博客,今天就再写一篇.写一写CPU中涉及到RAM的部件,如寄存器堆.数据存储器等. 大家应该在大一刚接触到计算机的时候就知道R ...

  9. mongo aggregate 删除重复数据

    $group 按照什么排序, 关照 _id 这个是排序的依据 $match 这个从排序的结果内抽取 count 大于一的 allDiskUse  如果内存配置比较小, 设置这个才能运行, 否则会崩. ...

  10. jsp视频如何播放

    网站开发小白们对如何插入视频有较大的困扰,一段时间不知道从何下手,想在数据库里面直接导入,但没能成功,后又尝试直接在myeclipse里面直接放入视频. 对于不同的播放器,视频的格式也有要求,建议使用 ...