0

I am implementing a circuit breaker solution to my code using the Spring-Breaker project and was writing the test cases for the same.

Consider the following example:

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

I need to test methodA and make it fail using CircuitBreaker timeout so I wrote a test class that mocks this.

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

@Test
testMethodA() {
    methodA();
}

How do I make sure I call the methodA() but also mock the gatewayServiceCall.

I hope the question was clear. Please let me know if it wasn't. I'll try to elaborate further.

Thanks.

Alexandre Jacob
  • 2,993
  • 3
  • 26
  • 36
Caffeinated Coder
  • 670
  • 1
  • 8
  • 24

1 Answers1

1

You could write an Answer that sleeps:

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);
MarkOfHall
  • 3,334
  • 1
  • 26
  • 30
  • Steve, that's the problem. The test ends up calling methodA() but executes the gateway service call in the original class and not the mocked one. – Caffeinated Coder Sep 23 '15 at 16:15
  • 1
    So, you question has nothing to do with Circuit Breakers and timeout? You just don't know how to use Mockito? – MarkOfHall Sep 23 '15 at 16:29