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. python第三方库------jieba库(中文分词)

    jieba“结巴”中文分词:做最好的 Python 中文分词组件 github:https://github.com/fxsjy/jieba 特点支持三种分词模式: 精确模式,试图将句子最精确地切开, ...

  2. SpringBoot配置Swagger实例(POST接收json参数)

    工程目录结构: 首先,引入jar包,只需要以下两个即可 <dependency> <groupId>io.springfox</groupId> <artif ...

  3. react-redux-store

    store是联系state 和 reducer的部分 Store 有以下职责: 维持应用的 state: 提供 getState() 方法获取 state: 提供 dispatch(action) 方 ...

  4. vs2017 无法提交到tfs的 git存储库

    tfs 是2018版本 使用git 工具是可以提交成功. 使用vs2017的 就会一直提示 授权失败 也可以使用新安装的git https://blog.csdn.net/Meteor_s/artic ...

  5. SVN分支与合并【超详细的图文教程】(转载)

    SVN分支与合并 一. 分支与合并的概念 二. SVN分支的意义 三. 如何创建分支与合并分支 一.分支与合并的概念: 分支:版本控制系统的一个特性是能够把各种修改分离出来放在开发品的一个分割线上.这 ...

  6. Spring Cloud各个组件的配套使用

    我们从整体上来看一下Spring Cloud各个组件如何来配套使用:  从上图可以看出Spring Cloud各个组件相互配合,合作支持了一套完整的微服务架构. 其中Eureka负责服务的注册与发现, ...

  7. WPF实现按钮鼠标停留样式的一个坑

    弄了个按钮鼠标停留样式,发现把它应用到某些窗体的Button上会发生样式模糊的问题,而其它窗体又不会. 百思不得其解,真是活久见. 后来发现是跟包着Button的容器控件有关,只要是那些会自适应的容器 ...

  8. /usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用

     LIBRARIES += boost_thread stdc++ boost_regex     https://github.com/rbgirshick/fast-rcnn/issues/52

  9. 花了2小时写bug

    程序员的工作,写bug,修bug,改bug 写了2小时逻辑关系,没写明白 比昨天多了一个返回上一层的功能 也很简单,清除下数组内容即可 emm..明天继续深究吧 dic = { "植物&qu ...

  10. background属性冲突导致的部分浏览器背景图片不显示问题

    前几天在项目中遇到了一个让人摸不着头脑的bug,测试说页面显示有点问题并发了截图, 正常的显示状态是这样 首先我自信地用自己的手机检查了一下,没有问题,问清楚后得知是UC浏览器中出现的,UC的内核是u ...