gpt4 book ai didi

java - 测试期间 spring webflux 中的全局异常处理

转载 作者:行者123 更新时间:2023-12-05 02:58:23 27 4
gpt4 key购买 nike

我正在使用 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
}

这是我的 Controller 建议:

@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);
}
}

最佳答案

如果您阅读 documentation对于 @WebFluxTest,它指出:

Annotation that can be used for a Spring WebFlux test that focuses only on Spring WebFlux components.

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to WebFlux tests (i.e. @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, and WebFluxConfigurer beans but not @Component, @Service or @Repository beans).

Typically @WebFluxTest is used in combination with @MockBean or @Import to create any collaborators required by your @Controller beans.

If you are looking to load your full application configuration and use WebTestClient, you should consider @SpringBootTest combined with @AutoConfigureWebTestClient rather than this annotation.

这意味着

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

不是您在这里使用的。 @WebFluxTest 注释不加载 @Component@Service@Repository

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

您的选择似乎是:

  • 加载 MyController.class 然后模拟该类具有的任何依赖项 (MyServiceImpl.class)
  • 使用 @SpringBootTest 加载一个完整的 Context 而不是结合@AutoConfigureWebTestClient

关于java - 测试期间 spring webflux 中的全局异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085457/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com