How to start the async and get the result at the same time?

706 views
Skip to first unread message

Durim Kryeziu

unread,
Oct 27, 2017, 10:20:22 AM10/27/17
to REST assured
Hi guys,

I want to use REST Assured library instead of MockMvc api, but I have a problem:

The return type of the rest controller method on my spring app is 
Observable<MyObject>
so when I do like this:

given()
 
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
 
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
 
.body(myContent())

 
.when()
 
.async()
 
.post(BASE_URI)

 
.then()
 
.apply(print())
 
.body("id", equalTo("foo"))
 
.expect(status().isOk());
the test passes but I got this in the console:

```Async:
    Async started = false```

On the other hand, when I remove the .async() from the method chain, the Async started = true but the test fails because of the result ?

Is it possible to have the the Async started = true and test the http status code the result as well ?

Thans in advance

Johan Haleby

unread,
Oct 28, 2017, 3:57:20 AM10/28/17
to rest-a...@googlegroups.com
Hi, 

Unfortunately I'm not sure how to solve it. Do you know how to solve it using vanilla MockMvc? If so maybe that would provide some hints.

/Johan 

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Durim Kryeziu

unread,
Oct 30, 2017, 4:32:02 AM10/30/17
to REST assured
Hi Johan,

Thank you for your response!

Here you have the code with vanilla MockMvc and its output:

MvcResult result = this.mockMvc
      .perform(
        post(BASE_URI)
          .accept(MediaType.APPLICATION_JSON_UTF8)
          .contentType(MediaType.APPLICATION_JSON_UTF8)
          .content(myContent())
      )
      .andDo(print())
      .andExpect(status().isOk()) // this is the status code of the MockHttpServletResponse
      .andReturn();

    this.mockMvc.perform(asyncDispatch(result))
      .andExpect(status().isOk()) // this is the status code of the Async
      .andExpect(jsonPath("$.id").value("foo"));

Output:
MockHttpServletRequest:
      HTTP
Method = POST
     
Request URI = /api/my/uri
       
Parameters = {}
         
Headers = {Accept=[application/json;charset=UTF-8], Content-Type=[application/json;charset=UTF-8]}


Handler:
             
Type = com.my.Controller
           
Method = public rx.Observable<com.my.AggregateId> com.my.Controller.create(java.lang.String,java.lang.String,com.my.Dto)


Async:
   
Async started = true
     
Async result = AggregateId(id=foo)


Resolved Exception:
             
Type = null


ModelAndView:
       
View name = null
             
View = null
           
Model = null


FlashMap:
       
Attributes = null


MockHttpServletResponse:
           
Status = 200
   
Error message = null
         
Headers = {}
     
Content type = null
             
Body =
   
Forwarded URL = null
   
Redirected URL = null
         
Cookies = []

I also asked the same question in the Stackoverflow days before as well:
https://stackoverflow.com/q/46705412/5695421 

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages