Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/geowarin/spring-spock-mvc
A thin layer over spring-test-mvc to make tests more spock-firendly
https://github.com/geowarin/spring-spock-mvc
Last synced: about 4 hours ago
JSON representation
A thin layer over spring-test-mvc to make tests more spock-firendly
- Host: GitHub
- URL: https://github.com/geowarin/spring-spock-mvc
- Owner: geowarin
- Created: 2015-09-16T11:20:09.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-21T15:54:52.000Z (almost 9 years ago)
- Last Synced: 2023-04-09T10:55:52.115Z (over 1 year ago)
- Language: Groovy
- Size: 55.7 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Spring-spock-mvc
A thin layer over spring-test-mvc to make tests more spock-friendly
# Motivation
Spring mvc tests are great but the fluent API is not a naturally
fit with spock.This is why I created a very small layer over spring-test-mvc.
It makes tests look nicer in groovy, using the capabilities of the language like JSON parsing.By separating the requests from the assertions, we can use the spock DSL
to write beautiful assertions.Since most API use Json, the library is converting data to and from JSON directly.
There is no support for other media types like XML.# Setup
My recommendation is to create a base class for your specifications
```groovy
@ContextConfiguration(
loader = SpringApplicationContextLoader,
classes = [MyApplication]
)
abstract class AbstractMvcSpec extends SpockMvcSpec {@Override
MockMvc buildMockMvc(WebApplicationContext wac) {
MockMvcBuilders.webAppContextSetup(wac).build()
}
}
```# Usage
```groovy
class AuthenticationSpec extends AbstractMvcSpec {def "unauthenticated users cannot get resource"() {
when:
def res = get('/api/simple')then:
res.status == HttpStatus.FORBIDDEN
}def "get session"() {
when:
def res = get('/api/session', new RequestParams(authToken: 'secretToken'))then:
res.status == HttpStatus.OK
res.json.username == 'user'
}def "post example"() {
when:
def res = post('/api/session', [someData: 'data'])then:
res.status == HttpStatus.OK
res.json.data == 'data'
}def "headers example"() {
when:
def res = post('/api/session', new RequestParams(headers: ['x-auth-token': 'token']))then:
res.status == HttpStatus.OK
}
}
```# Dependency
This lib depends on spock and groovy.
If you use spring-boot the dependencies versions are optional.Add the following to your build:
```groovy
compile 'org.codehaus.groovy:groovy'testCompile 'org.spockframework:spock-spring'
testCompile 'com.geowarin:spring-spock-mvc:0.2.1'
```## Availability
This package is hosted on https://bintray.com/bintray/jcenter[jcenter]
It is therefore available directly from a groovy grape.
You can also include the `jcenter()` repository in your gradle script.
Or add a bunch of XML in your pom.xml.
# License
MIT