Powermockito is required when:
----------------------------------------------------
- We are unable to mock static methods using normal mockito API.
- We want to test final classes / methods
- We are unable to test private methods.
- We want to mock constructor.
Powermockito limitations:
-------------------------------------------
- Currently there are no limitations with this API.
Creating mock object with PowerMockito:
-------------------------------------------------------------------
//similar to Mockito
Singleton mockedObj=PowerMockito.mock(Singleton.class);
//writing stub methods is same as Mockito
Mock static method example:
-----------------------------------------------
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.nagarjuna.java.junit.A;
import com.nagarjuna.java.junit.B;
import com.nagarjuna.java.junit.Singleton;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class, B.class, Singleton.class })
public class TestPowerMock {
@Before
public void init() {
PowerMockito.mockStatic(Singleton.class);
PowerMockito.when(Singleton.getOwnerName()).thenReturn("Suhail");
}
@Test
public void test() {
System.out.println(Singleton.getOwnerName());
}
}
Mock private static method example:
--------------------------------------------------
//Similar to mock normal methods..
http://code.google.com/p/powermock/wiki/MockitoUsage13
Mock void static method example:
-------------------------------------------------------
PowerMockito.doThrow(new NullPointerException()).when(Singleton.class, "go");
Singleton.go(); // will throw NullPointerException
Important Note:
- Partial mock can be done in both Mockito and PowerMockito API using spy concept.
Powermockito maevn depedency:
------------------------------------------------------
1.5.4
junit
junit
4.11
org.powermock
powermock-module-junit4
${powermock.version}
org.powermock
powermock-module-junit4-common
${powermock.version}
org.powermock
powermock-api-mockito
${powermock.version}
org.powermock
powermock-api-support
${powermock.version}
org.powermock
powermock-core
${powermock.version}
org.powermock
powermock-api-easymock
${powermock.version}
cglib
cglib
2.2.2
easymock
easymock
2.0
VeryGood Resource: http://www.packtpub.com/article/mocking-static-methods
----------------------------------------------------
- We are unable to mock static methods using normal mockito API.
- We want to test final classes / methods
- We are unable to test private methods.
- We want to mock constructor.
Powermockito limitations:
-------------------------------------------
- Currently there are no limitations with this API.
Creating mock object with PowerMockito:
-------------------------------------------------------------------
//similar to Mockito
Singleton mockedObj=PowerMockito.mock(Singleton.class);
//writing stub methods is same as Mockito
Mock static method example:
-----------------------------------------------
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.nagarjuna.java.junit.A;
import com.nagarjuna.java.junit.B;
import com.nagarjuna.java.junit.Singleton;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class, B.class, Singleton.class })
public class TestPowerMock {
@Before
public void init() {
PowerMockito.mockStatic(Singleton.class);
PowerMockito.when(Singleton.getOwnerName()).thenReturn("Suhail");
}
@Test
public void test() {
System.out.println(Singleton.getOwnerName());
}
}
Mock private static method example:
--------------------------------------------------
//Similar to mock normal methods..
http://code.google.com/p/powermock/wiki/MockitoUsage13
Mock void static method example:
-------------------------------------------------------
PowerMockito.doThrow(new NullPointerException()).when(Singleton.class, "go");
Singleton.go(); // will throw NullPointerException
Important Note:
- Partial mock can be done in both Mockito and PowerMockito API using spy concept.
Powermockito maevn depedency:
------------------------------------------------------
VeryGood Resource: http://www.packtpub.com/article/mocking-static-methods