json - Testing Spring asyncResult() and jsonPath() together -


i'm using restful url kick off long-running backend process (it on cron schedule, want ability kick off manually).

the code below works , see result in browser when test manually.

@responsebody @requestmapping(value = "/trigger/{jobname}", method = requestmethod.get) public callable<triggerresult> triggerjob(@pathvariable final string jobname) {      return new callable<triggerresult>() {         @override         public triggerresult call() throws exception {             // code goes here locate relevant job , kick off, waiting result             string message = <result job>;             return new triggerresult(success, message);         }     }; } 

when test without callable used code below , works (i changed expected error message simplify post).

mockmvc.perform(get("/trigger/job/xyz"))     .andexpect(status().isok())     .anddo(print())     .andexpect(jsonpath("status").value("success"))     .andexpect(jsonpath("message").value("a meaningful message appears")); 

when added callable not work. tried below did not work. else had success?

mockmvc.perform(get("/trigger/job/xyz"))     .andexpect(status().isok())     .anddo(print())     .andexpect(request().asyncresult(jsonpath("status").value("success")))     .andexpect(request().asyncresult(jsonpath("message").value("a meaningful message appears"))); 

below relevant part print(). looks mockmvc can't untangle json correctly in case (even though works in browser)? when without callable see full json.

mockhttpservletrequest:      http method =      request uri = /trigger/job/xyz       parameters = {}          headers = {}           handler:             type = foo.bar.web.controller.triggerjobcontroller           method = public java.util.concurrent.callable<foo.bar.myproject.web.model.triggerresult> foo.bar.myproject.web.controller.triggerjobcontroller.triggerjob(java.lang.string)             async:  async started = true       async result = foo.bar.myproject.web.model.triggerresult@67aa1e71   resolved exception:             type = null      modelandview:        view name = null             view = null            model = null          flashmap:  mockhttpservletresponse:           status = 200    error message = null          headers = {}     content type = null             body =     forwarded url = null   redirected url = null          cookies = [] 

bud's answer helped point me in right direction didn't quite work because did not wait async result. since posting question, spring-mvc-showcase samples (https://github.com/springsource/spring-mvc-showcase) have been updated.

it seems in first part of call when retrieve mvcresult, need assert on asyncresult() , in case of json pojo mapping need assert on actual type (not json). needed add third line below bud's answer, rest works.

mvcresult mvcresult = this.mockmvc.perform(get("/trigger/job/xyz"))     .andexpect(request().asyncstarted())     .andexpect(request().asyncresult(instanceof(triggerresult.class)))     .andreturn();  this.mockmvc.perform(asyncdispatch(mvcresult))     .andexpect(status().isok())     .andexpect(content().contenttype(mediatype.application_json))     .andexpect(jsonpath("status").value("success"))     .andexpect(jsonpath("message").value("a meaningful message appears")); 

note: instanceof() org.hamcrest.corematchers.instanceof. access hamcrest libraries include latest hamcrest-library jar.

for maven ...

    <dependency>         <groupid>org.hamcrest</groupid>         <artifactid>hamcrest-library</artifactid>         <version>latest version here</version>         <scope>test</scope>     </dependency> 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -