gpt4 book ai didi

java - spring-mvc:如何使用 mockMvc 测试 Rx 响应?

转载 作者:行者123 更新时间:2023-11-28 20:28:46 33 4
gpt4 key购买 nike

我的 Controller 是:

import rx.Single;
...

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

而且效果很好。但我无法测试它。我试过:

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

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

但它失败了:

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...

那么:如何使用 spring mvc 测试 rx.Single

最佳答案

我找到了答案。当您创建 mockMvc 对象时,为 Single 添加一个处理程序:

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

关于java - spring-mvc:如何使用 mockMvc 测试 Rx 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177473/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com