piotrek piotrek - 1 month ago 26
Java Question

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

my controller is:

import rx.Single;
...

@GetMapping
Single<List<MyType>> fetchFromDB() {
return Single
.fromCallable(() -> dao.fetch())
.subscribeOn(Schedulers.io());
}


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

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

String result = mvc
.perform(asyncDispatch(asyncResult))
.andReturn().getResponse().getContentAsString()


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
rx.Single
with spring mvc?

Answer

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

return MockMvcBuilders.standaloneSetup(controller)
                .setCustomReturnValueHandlers(new SingleReturnValueHandler())
Comments