如何使用@WebfluxTest 测试异常 [英] How to test exception with @WebfluxTest

查看:38
本文介绍了如何使用@WebfluxTest 测试异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 @WebfluxTest 测试我的控制器的异常路径,但它总是抛出服务器异常,然后返回 500 错误.

I am trying to test exception path for my controller with @WebfluxTest, but it is always throws a server exception and then return a 500 error.

测试代码是这里:

@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 处理.

The PostNotFoundException is caught in the background correctly, but it is not handled by my custom RestExceptionHandler in my @WebfluxTest test.

推荐答案

对于这个问题的未来听众​​,这个问题被提出为 一个针对 Spring Boot 问题跟踪器的问题 由问题作者提出,并已通过添加对 WebExceptionHandler 的支持解决在 @WebFluxTest 中,所以有问题的代码应该可以正常工作,因为 在撰写本文时尚未发布的 Spring Boot 2.1.0.M3.

For the future audience for this question, this question was raised as an issue against the Spring Boot issue tracker by the author of the question and has been resolved via adding support for WebExceptionHandler in @WebFluxTest, so the code in question should just work since the Spring Boot 2.1.0.M3 which is not released yet at the time of writing.

这篇关于如何使用@WebfluxTest 测试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆