在测试期间在Spring Webflux中进行全局异常处理

消防手套:

我正在使用Spring Webflux开发服务。我使用实现了异常处理@ControllerAdvice它工作得很好,但是当我运行集成测试时,似乎@ControllerAdvice未加载带注释的组件,从而导致以下响应:

{
   "timestamp":"2019-11-28T08:56:47.285+0000",
   "path":"/fooController/bar",
   "status":500,
   "error":"Internal Server Error",
   "message":"java.lang.IllegalStateException: Could not resolve parameter [1] in protected org.springframework.http.ResponseEntity<it.test.model.Response> it.test.exception.ExceptionHandlerController.handleServiceException(java.lang.Exception,org.springframework.web.context.request.WebRequest): No suitable resolver
}

这是我的控制器建议:

@ControllerAdvice
public class ExceptionHandlerController extends ResponseEntityExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(ExceptionHandlerController.class);

    @ExceptionHandler
    protected ResponseEntity<Response> handleServiceException(Exception ex, WebRequest request) {

        this.logger.error("Error occurred: \"{}\"", ex.getMessage());

        Response<Foo> response = new Response<>(new Foo(),
                "generic error",
                HttpStatus.INTERNAL_SERVER_ERROR);

        return new ResponseEntity<>(response, null, HttpStatus.OK);
    }
}

这是我的综合测试课

@ExtendWith(SpringExtension.class)
@WebFluxTest(MyController.class)
@ContextConfiguration(classes = {MyController.class, MyServiceImpl.class, ExceptionHandlerController.class })
public class MyControllerIT {

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void testShouldFail() throws IOException {

        return this.webTestClient.get()
            .uri(uri)
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectBody()
            .jsonPath("$.statusCode").isEqualTo(500);
    }
}
托马斯·安道夫:

如果你读的文档@WebFluxTest它规定:

可以用于关注Spring WebFlux组件的Spring WebFlux测试的注释。

使用此批注将禁用完全自动配置,而是仅应用与WebFlux测试相关的配置(即@ Controller,@ ControllerAdvice,@ JsonComponent,Converter / GenericConverter和WebFluxConfigurer Bean,而不是@ Component,@ Service或@Repository bean)。

通常,@ WebFluxTest与@MockBean或@Import结合使用以创建@Controller bean所需的任何协作者。

如果要加载完整的应用程序配置并使用WebTestClient,则应考虑将@SpringBootTest与@AutoConfigureWebTestClient结合使用,而不要使用此注释。

这意味着

@ContextConfiguration(classes = {MyController.class, MyServiceImpl.class, ExceptionHandlerController.class })

不是什么ü在这里使用。@WebFluxTest标注不加载@Component@Service@Repository

它主要用于仅测试RestControllers及其建议。

您选择的选项似乎是:

  • 加载MyController.class,然后模拟此类具有的任何依赖项(MyServiceImpl.class)
  • 加载完整的上下文,@SpringBootTest而不是结合使用@AutoConfigureWebTestClient

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Spring进行集成测试期间模拟外部服务器

在Maven中进行集成测试期间启动外部流程

使用Spring Boot进行集成测试期间使用测试数据填充数据库

在端到端测试期间如何应用全局管道

测试期间记录日志和预期的异常

如何在测试期间获取httpretty以停止打印异常?

laravel中的Codeception在功能测试期间未显示异常

在lambda aws测试期间,抛出未找到类异常

在发现MSTest Framework测试期间键入负载异常

在XCode中进行UI测试期间无法访问自定义视图

在TestCafe中进行测试期间,在浏览器的控制台中运行命令

Spring集成:测试期间的BindException

要在Xcode中进行UI测试期间在测试运行后停止调试控制台的清除

如何定义在使用鼻子测试进行测试期间仅调用一次的设置方法?

使用硒自动化测试期间是否可能进行压力测试

使用Spring Boot + WebFlux进行全局错误处理

C#-测试期间出现奇数空引用异常,为什么会发生这种情况?

在Jenkins中进行JMeter负载测试期间获取服务器的CPU和内存使用情况图

如何在使用Xcode 11和iOS 13进行UI测试期间关闭UIActivityViewController

是否建议从训练RNN中保存最终状态以在测试期间对其进行初始化?

Github操作:在使用JavaFX 13进行测试期间无法打开DISPLAY

在CI服务器上进行单元测试期间,AVPlayer无法加载文件

使用chai-http与mocha-chai进行测试期间的UnhandledPromiseRejectionWarning

使用WebWorker线程进行高负载并发测试期间的分段错误

在单元测试期间填充Spring @Value

是否可以在单元测试期间禁用Spring的@Async?

在Spring的集成测试期间MultipartFile#getContentType返回null

在Spring Boot测试期间在@Configuration中替换@Value属性

在集成测试期间重置Spring-Boot