How do I mock out Stripe network calls for testing in Java

Viewed 1987

I'm trying to test a function that's making a Stripe api call Plan.retrieve("my_plan_id"). I'm using junit as my testing library and mockito as my mocking library. The problem here is that Plan.retrieve() is a static method, and mockito doesn't support mocking static methods.

I've tried mocking out the Plan class like so.

Plan plan = new Plan();
plan.setId("my_plan_id");
Plan mockedPlan = mock(Plan.class);
when(mockedPlan.retrieve("my_plan_id")).thenReturn(plan);
assertEquals(plan.getId(), myTestedFunction().getId());

This just results in an Stripe Authentication exception because I never gave Stripe an API Key.

What is the correct implementation for mocking Stripe api calls for testing in java? I can't seem to find any information on this.

Update

I was already aware of PowerMock, I was looking for a solution that involved using Mockito. Dawood ibn Kareem has pretty much answered my question. If I could mark that as the accepted answer I would. I admit that it was a pretty obvious answer, but for some reason it just didn't occur to me.

I also don't understand why people are downvoting my question, was it a bad question?

1 Answers
Related