0

我正在使用Spring-Breaker 项目为我的代码实现一个断路器解决方案,并为此编写测试用例。

考虑以下示例:

@CircuitBreaker
methodA() {
    //some code
    gatewayServiceCall()
    //some code
}

我需要测试 methodA 并使用 CircuitBreaker 超时使其失败,所以我编写了一个模拟这个的测试类。

setup() {
    gatewayService = mock(GatewayService.class);
    when(gatewayService.methodName().thenReturn(something);
}

@Test
testMethodA() {
    methodA();
}

我如何确保我调用了 methodA() 但也模拟了 gatewayServiceCall。

我希望这个问题很清楚。如果不是,请告诉我。我将尝试进一步详细说明。

谢谢。

4

1 回答 1

1

你可以写一个睡觉的答案:

final Foo fooFixture = new Foo();
Answer<Foo> answer = new Answer<Foo>() {

    @Override
    public Foo answer(InvocationOnMock invocation) throws Throwable {
        Thread.currentThread().sleep(5000);
        return fooFixture ;
    }
};
when(gatewayService.methodName()).thenAnswer(answer);
于 2015-09-23T15:30:25.343 回答