Alex Rivera | Logout

PowerMock: mocking of static methods (+ return original values in some particular methods)

Asked 2011-02-01T08:42:54.360
19

I use PowerMock 1.4.7 and JUnit 4.8.2

I need to mock only some static methods and I want others (from the same class) just to return original value. When I mock with mockStatic and don't call when().doReturn() all static methods return their defaults - like null when returning Object or false when returning boolean...etc. So I try to use thenCallRealMethod explicitly on each static method to return default implementation (means no mocking/ no fakes) but I don't know how to call it on every possible arguments variations (= I want for every possible input call original method). I only know how to mock concrete argument variation.

Edit
Report

1 Answer

18

You can also use the stubbing API:

stub(method(MyStaticClass.class, "getB")).toReturn("B");

Edit:

To use stub and method statically import methods from these packages:

  1. org.powermock.api.support.membermodification.MemberModifier
  2. org.powermock.api.support.membermodification.MemberMatcher

For more info refer to the documentation

answered 2012-01-10T19:25:53.460

Your Answer