Understanding @PrepareForTest
PrepareForTest is very powerful tool that unlocks class manipulation capabilities by modifying class to byte code level.
PrepareForTest annotation
Mock static methods:
@RunWith(PowerMockRunner.class)
@PrepareForTest({
ClassWithStaticMethods.class,
})
public class ClassToTest {
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(ClassWithStaticMethods.class);
PowerMockito.when(ClassWithStaticMethods.staticMethod()).thenReturn(someValue);
}
}Mock final classes/methods:
Inject with PowerMockito.whenNew()
To suppress(), stub() or replace() any method of a Class. You can modify behavior of methods declared in any class, You can:
suppress(), stub() or replace() any method of a Class. You can modify behavior of methods declared in any class, You can:Last updated