1

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?

piotrek
  • 13,982
  • 13
  • 79
  • 165

2 Answers2

3

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

return MockMvcBuilders.standaloneSetup(controller)
                .setCustomReturnValueHandlers(new SingleReturnValueHandler())
piotrek
  • 13,982
  • 13
  • 79
  • 165
1

if you use MockMvcBuilders.webAppContextSetup(context) instead, you can add the handler in your WebMvcConfigurerAdapter:

public class Config extends WebMvcConfigurerAdapter {

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(new SingleReturnValueHandler());
    }
}
D-rk
  • 5,513
  • 1
  • 37
  • 55