Java Reference
In-Depth Information
The class RESTClient is part of the HttpBuilder ( http://groovy.codehaus.org/modules/
http-builder/ ) project. I'll discuss that further in section 9.4 on Groovy clients, but for now
let me say it defines Groovy classes that wrap Java classes supplied by Apache's Ht-
tpClient project. The test therefore contains an attribute of type RESTClient , as fol-
lows:
RESTClient client
new RESTClient('http://localhost:1234/', ContentType.JSON)
The client points tothe properendpoint, andthe second argument specifies the content type
for the Accept header in the request. A GET request using this client returns an object
that can be interrogated for header properties as well as data:
def 'get request returns all people'() {
when:
def response = client.get(path: 'people')
then:
response.status == 200
response.contentType == 'application/json'
response.data.size() == 5
}
Other finder methods are tested similarly. To keep the tests independent, the insert and de-
lete methods are tested together; first a person is inserted, then it's verified, and then it's
deleted again. The test uses another feature of Spock: each block ( when / then / expect ,
and so on) can be given a string to describe its purpose. It's not exactly behavior-driven
development, but it's as close as Spock comes at the moment.
The insert and delete test looks like the following:
def 'insert and delete a person'() {
given: 'A JSON object with first and last names'
def json = [first: 'Peter Quincy', last: 'Taggart']
when: 'post the JSON object'
def response = client.post(path: 'people',
contentType: ContentType.JSON, body: json)
then: 'number of stored objects goes up by one'
getAll().size() == old(getAll().size()) + 1
response.data.first == 'Peter Quincy'
response.data.last == 'Taggart'
response.status == 201
Search WWH ::




Custom Search