java - @HystrixCommand 中未调用后备方法
问题描述
试图测试我的后备方法,但不幸的是它从未被调用:(某些类调用这样的方法:
@Service
@EnableCircuitBreaker
public class ServiceOne
{
@Inject
private HystrixService hystrixService;
public String callCommand(int id, String name)
{
return hystrixService.callCommand(id, name);
}
}
这就是我的 HystrixService 的样子:
@Service
public class HystrixService
{
@Inject
private ServiceTwo serviceTwo;
@Override
@HystrixCommand(fallbackMethod = "fallback")
public String callCommand(final int id, final String name)
{
return serviceTwo.callMethod(id, name);
}
public String fallback(final int id, final String name)
{
return "blablabla";
}
}
我的测试:
//class
@Inject
@Spy
private ServiceTwo serviceTwo;
@Inject
@Spy
private ServiceOne serviceOne;
@Test
public void test_fallBack()
{
doThrow(new RuntimeException()).when(serviceTwo).callMethod(1,
"name");
String res = serviceOne.callCommand(1, "name");
}
为什么我的 serviceOne 从不返回存根“blablabla”,它不断抛出 RuntimeException 并且从不使用回退方法。我做错了什么?
解决方案
推荐阅读
- django - 无法在 django 模型中添加新字段
- java - Spring Boot 模块未找到错误
- system.reactive - Rx.Net:在 SelectMany 中调用多个 IObservable
- angular - 我有 Firebase 身份验证 ID,如何在数据库列表中使用它?
- html - 为什么我的 .obj 模型没有显示?
- django - Django没有渲染正确的图像路径
- typescript-typings - Angular 4 - 如何创建多用户、多主布局模板
- unity3d - Unity 2017.3 构建资产包需要很长时间
- c++ - 可以采用 Eigen::vector 或 std::vector 的 C++ 模板函数
- mysql - Laravel MySQL 搜索。允许用户进行自定义布尔搜索