unit testing - How to mock rest-easy's asynchronous HTTP request? -


is there offical way mock rest-easy asynchronous http request?

the sample code:

@get @path("test") public void test(@suspended final asyncresponse response) {      thread t = new thread() {         @override         public void run()         {              try {                 response jaxrs = response.ok("basic").type(mediatype.application_json).build();                 response.resume(jaxrs);             }             catch (exception e)             {                 e.printstacktrace();             }         }      };     t.start(); } 

i offen mock rest-easy's request way:

@test public void test() throws exception {     /**      * mock      */     dispatcher dispatcher = mockdispatcherfactory.createdispatcher();      dispatcher.getregistry().addsingletonresource(action);     mockhttprequest request = mockhttprequest.get("/hello/test");     request.addformheader("x-forwarded-for", "122.122.122.122");     mockhttpresponse response = new mockhttpresponse();      /**      * call      */     dispatcher.invoke(request, response);      /**      * verify      */     system.out.println("receive content:"+response.getcontentasstring()); } 

but dosn't work. got badrequestexception during unit test.

what's right way mock rest-easy asynchronous http request?

by reading rest-easy's source code, find way work around asynchronous http request :

@test public void test() throws exception {     /**      * mock      */     dispatcher dispatcher = mockdispatcherfactory.createdispatcher();      dispatcher.getregistry().addsingletonresource(action);     mockhttprequest request = mockhttprequest.get("/hello/test");     request.addformheader("x-forwarded-for", "122.122.122.122");     mockhttpresponse response = new mockhttpresponse();     // add following 2 lines !!!     synchronousexecutioncontext synchronousexecutioncontext = new synchronousexecutioncontext((synchronousdispatcher)dispatcher, request, response );     request.setasynchronouscontext(synchronousexecutioncontext);      /**      * call      */     dispatcher.invoke(request, response);      /**      * verify      */     system.out.println("receive content:"+response.getcontentasstring()); } 

the output of response correct !!!


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -