编写测试时未为 feignClient 执行回退方法

问题描述

我想测试假客户端的回退行为,但遇到了下面描述的一些问题。

这是被模拟的假客户端。

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

而且该服务还有 RestExceptionHandler ,它使用了 controllerAdvice 注释。 它有一个处理顶级异常的方法

@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,但不起作用。我在这里错过了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...