Czy mogę zastąpić domyślną odpowiedź RESTClient „HttpResponseException” na> 399 kodów powrotu?
Używam GroovyRESTClient class to do write some (spock) Testy akceptacji dla Java WebServices, które pisałem.
Jedną z frustracji było testowanie odpowiedzi ...
200
Statusy są łatwe:
when: def result = callServiceWithValidParams()
then: result.status == 200
Ale z400+
Jestem zmuszony albo zawinąćtry-catch
lub test naHttpResponseException
żeRESTClient
rzuca domyślnie.
when:
callWithInvalidParams()
then:
def e = thrown(Exception)
e.message == 'Bad Request'
To trochę OK, jeśli trochę frustrujące ... ale chcę zrobić lepiej.
Idealnie chciałbym, aby moje testy bardziej przypominały to (może być mylące, jeśli nie używasz 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
}
W powyższym przykładzie sprawa „Złe żądanie” nie powiedzie się. Zamiast zwracać wartość,restClient.get()
rzucaHttpResponseException