fallback方法

krugob8w  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(184)

我想测试外国客户机的回退行为,但有一些问题描述如下。
这就是被嘲笑的外国客户。

@ReactiveFeignClient(
    name = "discount-service",
    url = "${discountservice.base.url}",
    fallbackFactory = DiscountFallBackFactory.class)
public interface DiscountClient {
@PostMapping(path = "/cartDetails", headers = "x-code=${common.x-code}")
  Mono<DiscountResponse> calculateDiscount(@RequestBody DiscountRequest discountRequest);
}

对此的回退是

@Override
  public Mono<DiscountResponse> calculateDiscount(DiscountRequest discountRequest) {
    log.error(
        "Fallback occurred in calculateDiscount Method for request : {}. Details:",
        discountRequest,
        throwable);
    if (throwable instanceof FeignException) {
      return Mono.error(new CheckoutOrchestratorException((FeignException) throwable));
    } else {
      return Mono.error(throwable);
    }
  }

在测试中我是这样做的。

FeignException feignException = new FeignException.BadRequest("some message", request, error2.toString().getBytes());
Mockito.when(discountClient.calculateDiscount(Mockito.any())).thenThrow(feignException);

该服务还具有使用controlleradvice注解的restexceptionhandler。它有一个处理顶级异常的方法。

@ExceptionHandler(Exception.class)
  protected ResponseEntity<Object> handleException(Exception ex) {
    log.error("Failed", ex);
    return buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
  }

现在,在运行测试时,流不会回退,而是直接返回到restexceptionhandler的上述方法。尝试使用feign.hystrix.enabled=true,但不起作用。我错过了什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题