Caffeinated Coder
Caffeinated Coder

Reputation: 680

Mockito testing a method that has a step that calls a gateway service

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.

Upvotes: 0

Views: 860

Answers (1)

MarkOfHall
MarkOfHall

Reputation: 3353

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);

Upvotes: 1

Related Questions