46

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 real methods, except for those methods I explicitly mock.

Basically:

@Before
public void setupStaticUtil() {
  PowerMockito.mockStatic(StaticUtilClass.class);

  // mock out certain methods...
  when(StaticUtilClass.someStaticMethod(anyString())).thenReturn(5); 

  // Now have all OTHER methods call the real implementation???  How do I do this?
}

The problem I'm running into is that within StaticUtilClass the method public static int someStaticMethod(String s) unfortunately throws a RuntimeException if supplied with a null value.

So I can't simply go the obvious route of calling real methods as the default answer as below:

@Before
public void setupStaticUtil() {
  PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static methods

  // The below call to someStaticMethod() will throw a RuntimeException, as the arg is null!
  // Even though I don't actually want to call the method, I just want to setup a mock result
  when(StaticUtilClass.someStaticMethod(antString())).thenReturn(5); 
}

I need to set the default Answer to call real methods on all other static methods after I mock the results from the method I'm interested in mocking.

Is this possible?

Baptiste Pernet
  • 3,318
  • 22
  • 47
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129

2 Answers2

71

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)
zibi
  • 3,183
  • 3
  • 27
  • 47
  • I don't see either a stub() or a mockStaticPartial() method in my PowerMockito class. Version 1.5. In my version 1.9.5 Mockito class, I see a stub() method here: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#stub(T) however reading the docs I found this: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#when(T) which notes that **when() is a successor of deprecated stub(Object)**, so I don't think that method is meant to be used anymore, and wouldn't do what I need regardless. – Tom Tresansky Feb 01 '13 at 17:46
  • mockStaticPartial is defined for EasyMock - I didn't catch that you were asking explicitly for Mockito - http://powermock.googlecode.com/svn/docs/powermock-1.5/apidocs/org/powermock/api/easymock/PowerMock.html – zibi Feb 01 '13 at 18:10
  • 1
    stub is defined in MemberModifier which is a superclass of PowerMockito - it is there! - http://powermock.googlecode.com/svn/docs/powermock-1.5/apidocs/org/powermock/api/support/membermodification/MemberModifier.html#stub%28java.lang.reflect.Method%29 – zibi Feb 01 '13 at 18:11
  • 4
    Hi! Could you help? How stub method with specific arguments? – Arthur Jul 27 '17 at 11:14
4

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());

Arun
  • 2,360
  • 2
  • 17
  • 23