unit-testing - 单元测试在执行期间冻结
问题描述
所以,我的问题在这里 - 我正在尝试为我的应用程序进行单元测试。我有 2 项服务,我们称它们为 Foo 和 Bar,而 Foo 只不过是 Bar 的代理。
因此,Foo 服务的路由器如下所示:
fun fooRoute(...) = coRouter {
. . .
GET("/something", fooHandler::findSomething)
. . .
}
向 Bar 服务发出请求的处理程序如下所示:
fun fooHandler(barClient: WebClient) {
. . .
suspend fun findSomething(request: ServerRequest): ServerResponse {
val response = barClient.get()
.uri(...)
.accept(...)
.awaitExchange()
. . .
return when (response.statusCode()) {
HttpStatus.OK -> {
ServerResponse
. . .
.bodyValueAndAwait(response.awaitBody<SomeType>())
}
else -> {
ServerResponse
. . .
.buildAndAwait()
}
}
}
. . .
}
当我这样写测试时:
. . .
private val barClient = mockk<WebClient>()
private val fooHandler = FooHandler(barClient)
private val fooRouter = FooRouter()
private val fooClient = WebTestClient.bindToRouterFunction(
fooRouter.fooRoute(
// mocks
. . .
fooHandler
)
).build()
@Nested
inner class FindSomething {
@Test
fun `returns OK`() = runBlocking {
val response = mockk<ClientResponse>()
val spec = mockk<WebClient.RequestHeadersUriSpec<*>>()
coEvery { response.awaitBody<SomeType>() } returns SomeType(...)
coEvery { spec.awaitExchange() } returns response
coEvery { barClient.get() } returns spec
fooClient
.get()
.uri(...)
.exchange()
.expectStatus().is2xxSuccessful
. . .
}
}
它只是永远冻结......好吧,我认为这是因为它周围有一些协程魔法,但因为我还是这个新手,我不明白这里到底发生了什么。有什么帮助吗?
解决方案
Welp,我通过根据这个答案编写自己的交换函数解决了这个问题。这是我如何做到这一点的一个例子:
. . .
fun setUpBarClient(exchangeFunction: ExchangeFunction) {
barClient = barClient.mutate()
.exchangeFunction(exchangeFunction)
.build()
// recreate fooHandler & fooClient
}
@Nested
inner class FindSomething {
@Test
fun `returns OK`() {
// this one from org.springframework.web.reactive.function.client.ExchangeFunction
val exchangeFunction = ExchangeFunction {
Mono.just(
ClientResponse.create(HttpStatus.OK)
.header(...)
.body(...)
.build()
)
}
setUpBarClient(exchangeFunction)
fooClient
.get()
.uri(...)
.exchange()
.expectStatus().is2xxSuccessful
. . .
}
}
推荐阅读
- java - 如何使用 ArrayAdapter 将获取的 json 数据显示到 listView 中
- java - 使用 Spring-Retry 的 Kotlin 未调用 @Recover
- git - git 结帐 -b
但不在 git log 中显示历史记录 - angular - Angular 5 中的动态组件和事件发射器
- svg - SVG 文件具有相同的宽度且没有视图框,但两者看起来大小不同
- angular - nrwl 在库之间创建共享模块
- html - 使用 ion-slide 时页面不会滑到下一页
- c# - Wix ServiceInstall 指定帐户
- asp.net-core - 没有正则表达式的 ASP.NET Core 2.1 中的 URL 重写
- java - Java - 将实体转换为 wsimport 对象