PowerMock, mock a static method, THEN call real methods on all other statics

前端 未结 2 1036
青春惊慌失措
青春惊慌失措 2020-12-01 13:38

I\'m setting up mocking a class\' static methods. I have to do this in a @Before-annotated JUnit setup method.

My goal is to setup the class to call re

相关标签:
2条回答
  • 2020-12-01 14:11

    Though I'm late to the party, but we can achieve partial mocking and override the default behavior of mocked object by explicitly specifying it.

    Below example show how we can make PowerMockito to call real methods if behavior isn't defined explicitly:

    e.g. PowerMockito.mockStatic(MyClass.class, new CallsRealMethods());

    0 讨论(0)
  • 2020-12-01 14:33

    What are you looking for is called partial mocking.

    In PowerMock you can use mockStaticPartial method.

    In PowerMockito you can use stubbing, which will stub only the method defined and leave other unchanged:

    PowerMockito.stub(PowerMockito.method(StaticUtilClass.class, "someStaticMethod")).toReturn(5);
    

    also don't forget about the

    @PrepareForTest(StaticUtilClass.class)
    
    0 讨论(0)
提交回复
热议问题