Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mocking final class in Java with PowerMockito
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyFinalClass.class)
public class MyFinalClassTest {
@Test
public void it_should_mock_final_class() {
MyFinalClass mockObject = PowerMockito.mock(MyFinalClass.class);
Mockito.when(mockObject.getName()).thenReturn("iskenderunlu");
// ...
}
}
final class MyFinalClass {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment