I have a class with a few static methods.I need to Mock these static methods. I know PowerMock does this,but I was not able to find any tutorials/materials that shed some light
Since Powermock Version 1.6.0, powermock allows the delegation of the test runner.
This allows the wrapping of the Spock test runner (Sputnik) within the PowerMock test runner framework. Sputnik will then start the test case specifications, and still allow the use of the PowerMock framework.
With JUnit4 and Powermock, I use the following template for accessing static classes.
The test class:
package mypackage;
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 org.powermock.modules.junit4.PowerMockRunnerDelegate
import org.spockframework.runtime.Sputnik
import spock.lang.Specification
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([MyStaticMethodClass.class])
class MyTestForClassTest extends Specification {
def myStaticMethodClass
def setup() {
PowerMockito.mockStatic(MyStaticMethodClass.class)
myStaticMethodClass= Mock(MyStaticMethodClass)
PowerMockito.when(MyStaticMethodClass.getInstance()).thenReturn(myStaticMethodClass)
}
@Unroll
def "#TestCase policy RF210 triggered"() {
given: "a product list for the policy"
myStaticMethodClass.someInstanceMethod() >> "my return value"
classUnderTest = new ClassUnderTest()
...
The dependencies
org.mockito
mockito-all
1.10.19
test
org.powermock
powermock-module-junit4
1.7.4
test
org.powermock
powermock-api-mockito
1.7.4
test
org.spockframework
spock-core
1.3-groovy-2.5
test
cglib
cglib-nodep
3.3.0
test