【问题标题】:How to test exception with @WebfluxTest如何使用@WebfluxTest 测试异常
【发布时间】: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


【解决方案1】:

对于这个问题的未来观众,这个问题是由问题的作者以an issue against the Spring Boot issue tracker 提出的,并且已经通过在@WebFluxTest 中添加对WebExceptionHandler 的支持来解决,所以有问题的代码应该可以正常工作the Spring Boot 2.1.0.M3 在撰写本文时尚未发布。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多