I know how to mock static methods from a class using PowerMock.
But I want to mock static methods from multiple classes in a test class using JUnit and PowerMock.
Can anyone tell me is it possible to do this and how to do it?
I know how to mock static methods from a class using PowerMock.
But I want to mock static methods from multiple classes in a test class using JUnit and PowerMock.
Can anyone tell me is it possible to do this and how to do it?
Just do @PrepareForTest({Class1.class,Class2.class})
for multiple classes.
@Test
@PrepareForTest({Class1.class, Class2.class})
public final void handleScript() throws Exception {
PowerMockito.mockStatic(Class1.class);
PowerMockito.mockStatic(Class2.class);
etc...
If you are using kotlin, the syntax is this
@PrepareForTest(ClassA::class, ClassB::class)
In java with powermock/junit, use @PrepareForTest({})
with as many static classes as you want as array ({}
).
@RunWith(PowerMockRunner.class)
@PrepareForTest({XmlConverterA.class, XmlConverterB.class})
class TransfersServiceExceptionSpec {
}
I have used powermock with in scala/junit, as scalatest does not have integration with powermock.
@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[XmlConverterA], classOf[XmlConverterB]))
class TransfersServiceExceptionSpec {
@Test
def test() {
}
}