首页 > 解决方案 > @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 并且从不使用回退方法。我做错了什么?

标签: javajunithystrixcircuit-breaker

解决方案


推荐阅读