Last active
October 10, 2016 15:00
-
-
Save frangarcia/8e2cf247f48a185506e1a28b7cf98de0 to your computer and use it in GitHub Desktop.
Spock specification to test a REST API endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabExclude('org.codehaus.groovy:groovy-all') | |
@Grab(group='org.spockframework', module='spock-core', version='1.1-groovy-2.4-rc-2') | |
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1') | |
import spock.lang.* | |
import groovyx.net.http.RESTClient | |
@Stepwise | |
class MyFirstSpec extends Specification { | |
@Shared | |
def client | |
@Shared | |
def todoId | |
def setupSpec() { | |
client = new RESTClient('http://grailsangularjs-frangarcia.rhcloud.com/api/') | |
} | |
def "Listing the todos"() { | |
when: | |
def resp = client.get(path:"todo") | |
then: | |
with(resp) { | |
data.size() == 3 | |
} | |
} | |
def "Creating a new todo"() { | |
when: | |
def resp = client.post(path:"todo", body:[title:'My first title', todoList:1], requestContentType:'application/json') | |
and: | |
todoId = resp.data.id | |
then: | |
with (resp) { | |
status == 201 | |
data.title == 'My first title' | |
data.todoList.id == 1 | |
data.todoList.name == "House" | |
} | |
} | |
def "Listing the todos after creating a new one"() { | |
when: | |
def resp = client.get(path:"todo") | |
then: | |
with(resp) { | |
data.size() == 4 | |
} | |
} | |
def "Returning the new todo"() { | |
when: | |
def resp = client.get(path:"todo/${todoId}", requestContentType:'application/json') | |
then: | |
with(resp) { | |
data.id == todoId | |
data.title == 'My first title' | |
} | |
} | |
def "Updating the last created todo"() { | |
when: | |
def resp = client.put(path:"todo/${todoId}", body:[title:'My first title modified'], requestContentType:'application/json') | |
then: | |
with(resp) { | |
data.title == 'My first title modified' | |
} | |
} | |
def "Deleting the todo created in previous test"() { | |
when: | |
def resp = client.delete(path:"todo/${todoId}", requestContentType:'application/json') | |
then: | |
resp.status == 204 | |
} | |
def "Listing the todos after deleting the new one"() { | |
when: | |
def resp = client.get(path:"todo") | |
then: | |
with(resp) { | |
data.size() == 3 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment