Last active
June 9, 2018 11:57
-
-
Save baybatu/cdc4bd972d21f981e7b4903077dd3cba to your computer and use it in GitHub Desktop.
Mocking final class in Java with PowerMockito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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