我正在使用Spring Data Reactive MongoDB存储库以非阻塞方式保存新的pojo。我的路由器功能是:
//other routes
.andRoute(POST("/article/json"), articleHandler::createArticle);处理函数是:
public Mono<ServerResponse> createArticle(ServerRequest request) {
Flux<Article> article = request.bodyToFlux(Article.class);
articleRepository.insert(article).subscribe();
return ServerResponse.ok().build();
}我的测试方法:
@Test
public void givenNewArticle_whenDataIsValid_thenSuccess() {
//create new article object
webTestClient.post()
.uri("/article/json")
.body(fromObject(article))
.exchange()
.expectStatus().isOk();
}如果我通过curl发送json数据,则应用程序工作正常。但是测试方法不起作用。并且日志中没有错误或任何插入文档事件:
2017-11-09 10:49:27.793 INFO 18224 --- [ Thread-4] org.mongodb.driver.connection : Opened connection [connectionId{localValue:6, serverValue:17563}] to 10.45.250.101:9017
2017-11-09 10:49:27.793 INFO 18224 --- [ Thread-5] org.mongodb.driver.connection : Opened connection [connectionId{localValue:5, serverValue:17562}] to 10.45.250.101:9017
2017-11-09 10:49:28.115 INFO 18224 --- [ Thread-11] .r.c.ReactiveWebServerApplicationContext : Closing org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext@4e423aa2: startup date [Thu Nov 09 10:49:23 MSK 2017]; root of context hierarchy
2017-11-09 10:49:28.117 DEBUG 18224 --- [ Thread-11] o.s.d.r.l.RedisMessageListenerContainer : Stopped RedisMessageListenerContainer
2017-11-09 10:49:28.127 INFO 18224 --- [ Thread-11] org.mongodb.driver.connection : Closed connection [connectionId{localValue:4, serverValue:17561}] to 10.45.250.101:9017 because the pool has been closed.
2017-11-09 10:49:28.128 INFO 18224 --- [ Thread-11] org.mongodb.driver.connection : Closed connection [connectionId{localValue:3, serverValue:17560}] to 10.45.250.101:9017 because the pool has been closed.
2017-11-09 10:49:28.129 INFO 18224 --- [ Thread-11] org.mongodb.driver.connection : Closed connection [connectionId{localValue:5, serverValue:17562}] to 10.45.250.101:9017 because the pool has been closed.
2017-11-09 10:49:28.129 INFO 18224 --- [ Thread-11] org.mongodb.driver.connection : Closed connection [connectionId{localValue:6, serverValue:17563}] to 10.45.250.101:9017 because the pool has been closed.
2017-11-09 10:49:28.132 INFO 18224 --- [ Thread-11] r.ipc.netty.tcp.BlockingNettyContext : Stopped HttpServer on /0:0:0:0:0:0:0:0:61441通过WebTestClient测试restful api的正确方法是什么?蒂娅!
发布于 2017-11-10 20:14:58
您的测试正在运行,并提示存在实际的错误。
一般规则:你不应该在一个返回反应式类型的方法中调用 subscribe 。你应该从头到尾构建一个反应式管道。
错误解释
在这种情况下,调用subscribe将触发保存操作,但不能保证该方法在完成后会返回。subscribe返回一个跟踪该操作完成情况(成功或错误)的Disposable。保存数据实际工作可能发生在不同的线程中。
这在使用curl手动完成时是有效的,因为在从服务器返回响应之后,您的应用程序仍然在运行。对于您的测试,应用程序在执行保存操作之前停止。保存操作尚未发生。
你可以用以下命令“修复”这个问题:
public Mono<ServerResponse> createArticle(ServerRequest request) {
Flux<Article> article = request.bodyToFlux(Article.class);
articleRepository.insert(article).subscribe().block();
return ServerResponse.ok().build();
}但是通过这个修复,你在你的反应式管道中间引入了一个阻塞操作--这是一个严重的性能问题。这样做可能会阻塞服务器的少数几个线程,并导致性能非常差。
修复此错误
修复看起来像这样(分解的东西比所需的更多,你可以让它更短):
public Mono<ServerResponse> createArticle(ServerRequest request) {
Flux<Article> articles = request.bodyToFlux(Article.class);
Flux<Article> savedArticles = articleRepository.insert(articles);
return savedArticles.then(ServerResponse.ok().build());
}与以前的解决方案相比,此修复程序具有两个优势:
onError*反应器操作符进行自定义)进入反应心态还远不是显而易见的,所以编写测试绝对是挑战你的假设的正确方式-你做了正确的决定。
https://stackoverflow.com/questions/47187870
复制相似问题