PowerMockito Unit Test, Static method mock and Constructor mock

The subject is mocking with PowerMockito framework. Mocking static methods, mocking constructor with no argument and arguments are exist,.

PowerMockito version : 1.6.4
Junit version : 4.12
Jdk = 1.7 or 1.8
github sourceCode is : github link


<dependency>
 <groupId>org.powermock</groupId>
 <artifactId>powermock-module-junit4</artifactId>
 <version>1.6.4</version>
 <scope>test</scope>
</dependency>

<dependency>
 <groupId>org.powermock</groupId>
 <artifactId>powermock-api-mockito</artifactId>
 <version>1.6.4</version>
 <scope>test</scope>
</dependency>



This is PowerMockitoTestClass:

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.groupId.test.powermockito.ContainerClass;
import com.groupId.test.powermockito.FactoryClass;
import com.groupId.test.powermockito.FooEntity;
import com.groupId.test.powermockito.ServiceClass;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceClass.class, FactoryClass.class})
public class PowerMockitoTest {

    @InjectMocks
    ContainerClass containerClass = new ContainerClass() ;
   
    @Before
    public void before(){
        MockitoAnnotations.initMocks(this);
    }
   
    @Test
    public void staticMethodTest(){
        //When you say mockStatic all static void methods are empty and non-void methods returns null
        PowerMockito.mockStatic(ServiceClass.class);
       
        String strValue = "mock result str";
        String result = null;
        PowerMockito.when(ServiceClass.stringMethod(Matchers.anyInt(), Matchers.anyString(), Matchers.any(FooEntity.class))).thenReturn(strValue );
   
        result = ServiceClass.stringMethod(1, "str", new FooEntity());
        assertTrue(result.equals(strValue));
       
        ServiceClass.staticVoidMethod();
    }
   
    @Test
    public void constructorTest() throws Exception{
        ServiceClass serviceClass = PowerMockito.mock(ServiceClass.class);
       
        PowerMockito.whenNew(ServiceClass.class).withNoArguments().thenReturn(serviceClass);
        containerClass.callNonStaticMethod();

        PowerMockito.whenNew(ServiceClass.class).withAnyArguments().thenReturn(serviceClass);
        containerClass.callNonStaticMethodWithParam();
    }
}



This main caller class:

public class ContainerClass {


    public void callStaticMethod(){
        Integer intValue = Integer.valueOf(1);
        String str = "str";
        FooEntity fooEntity = new FooEntity();

        ServiceClass.staticVoidMethod();
        ServiceClass.stringMethod(intValue, str, fooEntity);
    }

    public void callNonStaticMethod(){
        ServiceClass powerMockitoClass = FactoryClass.getPowerMockitoClass();
        powerMockitoClass.nonStaticMethod();
    }

    public void callNonStaticMethodWithParam(){
        ServiceClass powerMockitoClass = FactoryClass.getPowerMockitoClassWithArgument();
        powerMockitoClass.nonStaticMethod();
    }
}






This is ServiceClass:

public class ServiceClass {

    public ServiceClass() {
        throw new RuntimeException("exception message");
    }

    public ServiceClass(String str, FooEntity fooEntity) {
        throw new RuntimeException("exception message");
    }
   
    public static void staticVoidMethod(){
        throw new RuntimeException("exception message");
    }
   
    public static String stringMethod(Integer intValue, String str, FooEntity fooEntity){
        throw new RuntimeException("exception message");
    }
   
    public void nonStaticMethod(){
        throw new RuntimeException("exception message");
    }
}



This is factory class:

 public class FactoryClass {

    public static ServiceClass getPowerMockitoClass(){
        return new ServiceClass();
    }

    public static ServiceClass getPowerMockitoClassWithArgument(){
        return new ServiceClass("str", new FooEntity());
    }
}



This is FooEntity:

public class FooEntity {

}