Spock 测试 - RESTClient: HttpResponseException: Not Found

Spock test - RESTClient: HttpResponseException: Not Found

我想在 API returns 404 时为 GET 请求编写测试。

我的测试:

   def "Should return 404 - object deleted before"() {
        setup:
        def advertisementEndpoint = new RESTClient( 'http://localhost:8080/' )
        when:
        def resp = advertisementEndpoint.get(
                path: 'api/advertisement/1',
                contentType: groovyx.net.http.ContentType.JSON
        )
        then:
        resp.status == 404
    }

我的错误:

14:24:59.294 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely 14:24:59.305 [main] DEBUG groovyx.net.http.RESTClient - Response code: 404; found handler: org.codehaus.groovy.runtime.MethodClosure@312aa7c 14:24:59.306 [main] DEBUG groovyx.net.http.RESTClient - Parsing response as: application/json 14:24:59.443 [main] DEBUG org.apache.http.wire - << "ba[\r][\n]" 14:24:59.444 [main] DEBUG org.apache.http.wire - << "{"timestamp":1436358299234,"status":404,"error":"Not Found","exception":"com.pgssoft.exparo.web.ResourceNotFoundException","message":"No message available","path":"/api/advertisement/1"}" 14:24:59.445 [main] DEBUG org.apache.http.wire - << "[\r][\n]" 14:24:59.445 [main] DEBUG org.apache.http.wire - << "0[\r][\n]" 14:24:59.446 [main] DEBUG org.apache.http.wire - << "[\r][\n]" 14:24:59.446 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@2ab4bc72 14:24:59.446 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Connection can be kept alive indefinitely 14:24:59.449 [main] DEBUG groovyx.net.http.RESTClient - Parsed data to instance of: class groovy.json.internal.LazyMap

groovyx.net.http.HttpResponseException: Not Found at groovyx.net.http.RESTClient.defaultFailureHandler(RESTClient.java:263) at groovy.lang.Closure.call(Closure.java:423) at groovyx.net.http.HTTPBuilder.handleResponse(HTTPBuilder.java:503) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:218) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160) at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515) at groovyx.net.http.RESTClient.get(RESTClient.java:119) at AdvertisementTest.Should return 404 - object delete before(AdvertisementTest.groovy:79)

您需要一个 底层 HTTPBuilder 的故障处理程序。来自 HTTPBuilder javadoc:

You can also set a default response handler called for any status code 399 that is not matched to a specific handler. Setting the value outside a request closure means it will apply to all future requests with this HTTPBuilder instance:

http.handler.failure = { resp -> println "Unexpected failure: ${resp.statusLine}" }

因此:

@Grapes(
    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
)

import groovyx.net.*
import groovyx.net.http.*   

def restClient = new RESTClient('http://localhost/wrong')
restClient.handler.failure = { resp -> resp.status }
def response = restClient.get([:])
assert response == 404