【发布时间】:2018-06-18 03:58:51
【问题描述】:
我正在尝试使用 @WebfluxTest 测试我的控制器的异常路径,但它总是抛出服务器异常,然后返回 500 错误。
测试代码是here:
@Test
@Ignore // ignore it temporarily
public void getPostByNonExistedId_shouldReturn404() {
given(posts.findById("1"))
.willReturn(Mono.empty());
client.get().uri("/posts/1").exchange()
.expectStatus().isNotFound();
verify(this.posts, times(1)).findById(anyString());
verifyNoMoreInteractions(this.posts);
}
控制器代码为:
@GetMapping("/{id}")
public Mono<Post> get(@PathVariable("id") String id) {
return this.posts.findById(id).switchIfEmpty(Mono.error(new PostNotFoundException(id)));
}
PostNotFoundException 在后台被正确捕获,但在我的@WebfluxTest 测试中我的自定义RestExceptionHandler 没有处理它。
【问题讨论】:
-
你找到解决办法了吗?
-
@Krotz 我认为这应该是一个错误,传统的 mvc 测试效果很好。
标签: spring spring-boot mockito spring-webflux spring-boot-test