5

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?

Michael
  • 835
  • 2
  • 10
  • 24
  • 2
    This object `Plan plan = new Plan()` is not a mock, thus it will execute its inner methods (if any). You should also mock that object as well. – Luiggi Mendoza Jan 17 '19 at 23:56
  • 5
    The normal thing to do would be to wrap whichever part of the Stripe API you're using into your own wrapper class, without any static methods. Then mock that. – Dawood ibn Kareem Jan 18 '19 at 00:13
  • Okay. That makes a ton of sense. I will try doing that. – Michael Jan 18 '19 at 00:20
  • 1
    I just read your update. So what I described is basically 99Sono's answer to the question that fmatar linked to. Instead of posting my answer here, it makes more sense to flag this as a duplicate. I would recommend avoiding PowerMock if you possibly can. – Dawood ibn Kareem Jan 18 '19 at 02:55
  • Ahh, I see. I read that stackoverflow question when I did my initial research but I didn't scroll down enough to see 99Sono's answer. Thank you so much for the help! – Michael Jan 23 '19 at 18:52
  • There is also another option if you want to get into the details of the Stripe implementation: you can implement `StripeResponseGetter` and call `ApiResource.setStripeResponseGetter(instance);`. This is the class that then gets called to perform any requests to Stripe. I am currently looking to see if something like `okhttp3.mock.MockInterceptor` from OkHTTP would be useful her to record and validate expectations. Might be more work than it is worth though. – Shane Aug 05 '19 at 05:24

1 Answers1

0

Mockito has no support for mocking static methods, you can use PowerMock to achieve the task. Specifically you should be looking at: https://github.com/powermock/powermock/wiki/Mockito#mocking-static-method

A similar question has been answered in this thread: Mocking static methods with Mockito

fmatar
  • 3,490
  • 1
  • 15
  • 11
  • 1
    seems that now Mockito suppports static methods mocking: https://javadoc.io/static/org.mockito/mockito-core/3.5.13/org/mockito/Mockito.html#static_mocks . – funder7 Oct 14 '20 at 13:05