Praveen
Praveen

Reputation: 101

Mock static method that calls to external service

I have a class that has a static method that passes in request and that calls server to retrieve the response. is there a way to mock that since it unit test I do not want to make a service call.

String jsonResponse = getMeMyMoney(request)

protected static String getMeMyMoney(request)
{
response = executeService(request)
return response
}

I tried this which is supposed to bypass the method but it still went it. Any one knows how to do this

doReturn("1").when(TestClass.getMeMyMoney("S"));

Upvotes: 0

Views: 332

Answers (1)

Reins
Reins

Reputation: 1119

You cannot mock static methods with Mockito, it is also stated in FAQ.

Use PowerMock on top of Mockito.

PowerMockito.mockStatic(TestClass.class);
when(TestClass.getMeMyMoney("S")).thenReturn("1");

Upvotes: 2

Related Questions