piotrek piotrek - 1 year ago 325
Java Question

spring-mvc: how to test Rx responses with mockMvc?

my controller is:

import rx.Single;

Single<List<MyType>> fetchFromDB() {
return Single
.fromCallable(() -> dao.fetch())

and it works perfectly. but i can't tests it. i tried:

MvcResult asyncResult = mvc.perform(get("/")).andReturn()

String result = mvc

but it fails with:

java.lang.IllegalStateException: Async result for handler [rx.Single<java.util.List<MyType>> MyController.fetchFromDB()] was not set during the specified timeToWait=-1

at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:145)
at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:121)
at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(MockMvcRequestBuilders.java:246)
at MyControllerSpec.should fetch from db...

so: how to test
with spring mvc?

Answer Source

i found the answer. when you create your mockMvc object, add a handler for Single:

return MockMvcBuilders.standaloneSetup(controller)
                .setCustomReturnValueHandlers(new SingleReturnValueHandler())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download