spring - Spock test - RESTClient: HttpResponseException: Not Found -
i want write test request when api returns 404.
my test:
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 }
my error:
14:24:59.294 [main] debug o.a.h.impl.client.defaulthttpclient - connection can 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 kept alive indefinitely 14:24:59.449 [main] debug groovyx.net.http.restclient - parsed data instance of: class groovy.json.internal.lazymap
groovyx.net.http.httpresponseexception: not found @ groovyx.net.http.restclient.defaultfailurehandler(restclient.java:263) @ groovy.lang.closure.call(closure.java:423) @ groovyx.net.http.httpbuilder$1.handleresponse(httpbuilder.java:503) @ org.apache.http.impl.client.closeablehttpclient.execute(closeablehttpclient.java:218) @ org.apache.http.impl.client.closeablehttpclient.execute(closeablehttpclient.java:160) @ groovyx.net.http.httpbuilder.dorequest(httpbuilder.java:515) @ groovyx.net.http.restclient.get(restclient.java:119) @ advertisementtest.should return 404 - object delete before(advertisementtest.groovy:79)
you need failure handler underlying httpbuilder
. httpbuilder
javadoc:
you can set default response handler called status code 399 not matched specific handler. setting value outside request closure means apply future requests httpbuilder instance:
http.handler.failure = { resp -> println "unexpected failure: ${resp.statusline}" }
therefore:
@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
Comments
Post a Comment