Can PowerMock mock system classes like System or Math?
A) Yes
B) No
Answer:
A) Yes
Explanation:
Yes, PowerMock can mock system classes like System
or Math
. PowerMock’s ability to modify bytecode at runtime allows it to mock even final system classes, which are usually unmockable in other frameworks.
For example:
mockStatic(System.class);
when(System.currentTimeMillis()).thenReturn(123456789L);
In this example, the static method currentTimeMillis()
in the System
class is mocked to return a specific value.