在@HystrixCommand中不调用Fallback方法

时间:2018-05-15 15:40:44

标签: java junit hystrix circuit-breaker

尝试测试我的后备方法,但遗憾的是它永远不会被调用:( 某些类调用方法如下:

@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永远不会返回stub“blablabla”,它会不断抛出RuntimeException而永远不会使用fallback方法。我做错了什么?

0 个答案:

没有答案