1

I have written test cases for testing a function in class Main, called functionMain(). I have seen people using PowerMockito to test static functions in the class Main which is under test.

But in my case, functionMain() is using a static function from another class called Branch called staticBranchFunction().

I want to mock staticBranchFunction() inside the tests for the Main class.

This main function actually has calls to static functions from different classes Branch1, Branch2 etc.

Please help.

kaushalpranav
  • 1,725
  • 24
  • 39
  • 2
    Possible duplicate of [Mocking static methods with Mockito](https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito) – candied_orange Feb 17 '18 at 14:02
  • That actually answers how to test the static methods from the same class that is under test, but not the static functions from different classes. – kaushalpranav Feb 17 '18 at 14:05
  • 1
    Can you show us some code? – curlyBraces Feb 17 '18 at 14:48
  • yes little bit code will help us understand clear – VedantK Feb 17 '18 at 15:07
  • are you testing/invoking functionMain() from junit, and it has call tto other static methos which you want to mock ? – VedantK Feb 17 '18 at 15:09
  • The main problem here is the use of *static access*. The fact that you have problems to test is is an indication that **code using static access to other classes methods or variables is hard to reuse**. So the better way then surrendering to you bad design y using PowerMock would be to change this other classes method to non-static and *inject* an instances of that other class into your code under test (preferably as a *constructor parameter*). – Timothy Truckle Feb 18 '18 at 09:43

1 Answers1

1
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Boom.class})
public class DocTest {

    public String boomWrapper() {
        return Boom.detonate();
    }

    @Test
    public void testBoom() {
        mockStatic(Boom.class);
        when(Boom.detonate()).thenReturn("defused");
        String actual = boomWrapper();
        verifyStatic(Boom.class, times(1));
        Boom.detonate();
        assertEquals("defused", actual);
    }    
}

class Boom {
    private static final String BOOM = "Boom!";  
    public static String detonate() {
        return BOOM;
    }
}

Dependencies:

junit:junit:4.12  
org.mockito:mockito-core:2.13.0  
org.powermock:powermock-module-junit4:2.0.0-beta.5  
org.powermock:powermock-api-mockito2:2.0.0-beta.5  

Description:

For more supported versions, please read: Mockito + PowerMock, other supported frameworks Requirements:

  • List all static classes in @PrepareForTest({Boom.class}) separate by comma.
  • Mock all static classes by PowerMockito.mockStatic(Boom.class) separate by comma.
  • use regular mockito methods to setup your expectation, for example Mockito.when(Boom.detonate()).thenReturn("defused")
  • verify behavior by PowerMockito.verifyStatic(Boom.class, Mockito.times(1)); Boom.detonate(); Important: You need to call PowerMockito.verifyStatic(Boom.class) per method verification.

More details on PowerMock wiki.

uli
  • 671
  • 6
  • 15