Spock - Document -06 - Modules
Modules
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 @BootstrapWith
and 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>
Creates a
Mock
Creates a
Stub
Creates a
Spy
Wraps an existing bean with a
Spy
. Fails fast if referenced bean is not found.If you don’t want to use the special namespace support you can create the beans via the
SpockMockFactoryBean
The
mockNature
can beMOCK
,STUB
, orSPY
and defaults toMOCK
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
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的更多相关文章
- Spock - Document -02 - Spock Primer
Spock Primer Peter Niederwieser, The Spock Framework TeamVersion 1.1 This chapter assumes that you h ...
- Spock - Document -05 - Extensions
Extensions Peter Niederwieser, The Spock Framework TeamVersion 1.1 Spock comes with a powerful exten ...
- Spock - Document -04- Interaction Based Testing
Interaction Based Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Interaction-based ...
- Spock - Document - 03 - Data Driven Testing
Data Driven Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Oftentimes, it is useful ...
- Spock - Document -01- introduction & Getting Started
Introduction Peter Niederwieser, The Spock Framework TeamVersion 1.1 Spock is a testing and specific ...
- [Python] 06 - Modules --> Packages
故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...
- Apache的初中级面试题
--- 原文:[关于Apache的25个初中级面试题](http://www.oschina.net/translate/apache-interview-questions) Apache 求职面试 ...
- FILE SIGNATURES TABLE
FILE SIGNATURES TABLE 16 December 2017 This table of file signatures (aka "magic numbers") ...
- [转]bigbluebutton中文社区 / 开放API / bbb API
bigbluebutton中文社区 / 开放API / bbb API 创建会议 这个接口可以重复调用多次,而不会有副作用.这带来的好处就是能简化应用程序加会的流程,无论什么用户想要加会,都可以先创建 ...
随机推荐
- 棋牌平台开发教程之扎金花大小比较算法在php中的实现
PHP中扎金花比大小如何实现 在棋牌游戏中,不管是现实的还是线上的,扎金花无疑是最热门棋牌游戏之一,鄙人从小就酷爱扎金花,机缘巧合后面从事了IT行业,话不多说,直接进去正题吧. 扎金花两副牌的比较规则 ...
- maven 项目提示找不到javax.servlet.xxx问题解决
1.https://search.maven.org/search?q=g:javax.servlet%20AND%20a:javax.servlet-api&core=gav 2.下载3.1 ...
- history program(language)
1950与1960年代 有三个现代编程语言于1950年代被设计出来,这三者所衍生的语言直到今日仍旧广泛地被采用: Fortran (1955),名称取自"FORmula TRANslator ...
- Road of computer tec 01
回顾自己过去将近3年的学习经历 当初报考的时候,自己是真正喜欢这个专业的么? 当初报这个专业的时候,对计算机还没有什么很明显的概念,只知道这个专业以后是要每天面对电脑的,喜欢不喜欢谈不上吧,但是还蛮感 ...
- mybatis使用接口联合查询
一.先建立两个实体类和配置文件 配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE c ...
- 九、Linux上软件安装
1. 在Linux上安装JDK: [步骤一]:上传JDK到Linux的服务器. * 上传JDK * 卸载open-JDK java –version rpm -qa | grep java rpm - ...
- Miller Robbin测试模板(无讲解)
想着费马定理和二次探测定理就能随手推了. 做一次是log2n的. #include<bits/stdc++.h> using namespace std; typedef long lon ...
- flask 第四章 偏函数 Local空间转时间 myLocalStack RunFlask+request 请求上下文
1 . 偏函数 (partial) from functools import partial def func(*args,**kwargs): a=args b=kwargs return a,b ...
- Mysql数据字典导出
1.phpmyadmin中自带的数据字典导出 2.利用下面的脚本: <?php /** * 生成mysql数据字典 */ header("Content-type: text/html ...
- 2018-2019-2 20175224 实验一《Java开发环境的熟悉》实验报告
实验报告封面 实验内容与步骤 Java开发环境的熟悉-1 1.建立“自己学号exp1”的目录 2.在“自己学号exp1”目录下建立src,bin等目录 3.javac.java的执行在“自己学号exp ...