Which method is used to mock static methods using PowerMock?
A) mockStatic()
B) mock()
C) mockStaticMethods()
D) mockAllStatic()
Answer:
A) mockStatic()
Explanation:
The mockStatic()
method in PowerMock is used to mock static methods. This allows you to replace the implementation of static methods with mock behavior during unit tests.
For example:
import static org.powermock.api.mockito.PowerMockito.*;
mockStatic(ClassWithStaticMethods.class);
when(ClassWithStaticMethods.staticMethod()).thenReturn("Mocked Value");
In this example, the mockStatic()
method is used to mock the static method staticMethod()
in the ClassWithStaticMethods
class.