I'm using the Groovy RESTClient class to do write some (spock) Acceptance tests for Java WebServices I've been authoring.
One frustration I've had is in testing the responses...
200
Status's are easy:
when: def result = callServiceWithValidParams()
then: result.status == 200
But with 400+
I'm forced to either wrap in a try-catch
, or test for the HttpResponseException
that RESTClient
throws by default.
when:
callWithInvalidParams()
then:
def e = thrown(Exception)
e.message == 'Bad Request'
This is sort-of OK, if a little frustrating... but I want to do better.
Ideally, I want my tests to more resemble this (might be confusing if you don't use groovy/spock)
@Unroll
def "should return #statusCode '#status' Response"()
{
when:
def result = restClient.get(path: PATH, query: [param: parameter])
then:
result.status == statusCode
where:
status | statusCode | parameter
'OK' | 200 | validParam
'Bad Request' | 400 | invalidParam
}
In the above example, the 'Bad Request' case fails. Instead of returning a value, restClient.get()
throws HttpResponseException
callService(params)
as a wrapper around the client where theparams
values are maps of queryString params to be sent to the service. All of this is not really important though. The main point is that the RESTService returns aresult
for 200 responses, but throws an error for 400, 401, etc... so I cannot test the response in a standard way in mythen:
block – Predicative