我正在使用Groovy RESTClient类为Java WebServices写一些(spock)验收测试我一直在创作.
我遇到的一个挫折就是测试答案......
200 状态很简单:
when: def result = callServiceWithValidParams()
then: result.status == 200
Run Code Online (Sandbox Code Playgroud)
但是400+我被迫要么包装,要么默认try-catch测试HttpResponseException那个RESTClient抛出.
when:
callWithInvalidParams()
then:
def e = thrown(Exception)
e.message == 'Bad Request'
Run Code Online (Sandbox Code Playgroud)
这有点好,如果有点令人沮丧......但我想做得更好.
理想情况下,我希望我的测试更像这样(如果你不使用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
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,"错误请求"案例失败. …