Mastering Static Resource Mocking in Spock with PowerMock
This article provides a step‑by‑step guide on mocking static variables and static methods in the Spock testing framework using PowerMock, covering annotation setup, custom behavior definitions, and handling mixed scenarios where both static and instance mocks are required.
In a previous article the author covered basic mock object usage in Spock; this piece focuses on mocking static resources.
Static Resources
Static Variables
Static variable mocking is rare; simply assign the mock object to the static field and the scenario can be considered passed.
Static Methods
To mock static methods, combine PowerMock with Mockito. Add the following annotations to the test class:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([NewUtil.class, HttpBase.class])
@PowerMockIgnore(["javax.management.*"])
@SuppressStaticInitializationFor(["com.funtester.util.NewUtil", "com.funtester.util.HttpBase"])The annotations should be copied verbatim. @PrepareForTest lists the classes whose static methods will be mocked; @PowerMockIgnore skips checks for specified packages; @SuppressStaticInitializationFor prevents static initialization of the listed classes.
Within the test’s setup, mock the static classes:
PowerMockito.mockStatic(HttpBase.class)
PowerMockito.mockStatic(NewUtil.class)Define custom behavior for a static method:
PowerMockito.when(HttpBase.fetchServiceNames())
.thenReturn(["service-prod","api-pro","prod","service-prd","write-pro"])The syntax for defining static method behavior mirrors that for instance methods.
Mixed Scenarios
When a test needs to mock both static methods and regular object methods, PowerMock must be used because the additional class‑level annotations interfere with Spock’s native mocking capabilities.
Example test combining static and instance mocks:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([NewUtil.class, HttpBase.class])
@PowerMockIgnore(["javax.management.*"])
@SuppressStaticInitializationFor(["com.funtester.util.newinterface.NewUtil", "com.funtester.util.slowapi.HttpBase"])
class TaskScheduledTest extends Specification {
@Shared
def service = PowerMockito.mock(IService)
def drive = new TaskScheduled(IService: service, cid: "")
def setupSpec() {
PowerMockito.mockStatic(HttpBase.class)
PowerMockito.mockStatic(NewUtil.class)
PowerMockito.when(HttpBase.fetch()).thenReturn(["ood","ero"])
Mockito.when(newutil.filter(Mockito.any())).thenReturn(true)
Mockito.when(newser.selectAll()).thenReturn([
new NewInterface() {{ setUrl("/abc"); setNname("test"); setMethod("GET"); }},
new NewInterface() {{ setUrl("/abcd"); setNname("test"); setMethod("POST"); }},
new NewInterface() {{ setUrl("/abce"); setNname("test"); setMethod("GET"); }}
])
PowerMockito.when(NewUtil.getsAll(anyList(), anyBoolean()))
.thenReturn([new NewInterface() {{ setUrl("/abc"); setNname("test"); setMethod("GET"); }}])
}
def "Send"() {
given:
drive.send()
}
def "day"() { }
}Note: Newer versions of Mockito (artifact mockito-inline) support static mocking, but integrating it with Spock is problematic due to version mismatches between Spock, Mockito, and Groovy.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
