Created
April 28, 2015 14:17
-
-
Save YukiMatsumura/60ad4a2de5f84dd77982 to your computer and use it in GitHub Desktop.
AndroidStudio + PowerMock でstaticメソッドをmockする ref: http://qiita.com/Yuki_312/items/80e0cda79731eb56c769
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
// UnitTestにはtestCompileキーワードを使用 | |
testCompile 'junit:junit:4.12' | |
// PowerMockのライブラリを指定 | |
testCompile 'org.powermock:powermock-module-junit4:1.6.2' | |
testCompile 'org.powermock:powermock-api-mockito:1.6.2' |
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
testOptions { | |
unitTests.returnDefaultValues = true | |
} |
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
@RunWith(PowerMockRunner.class) | |
public class MyTestCase extends TestCase { |
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
@RunWith(PowerMockRunner.class) | |
@PrepareForTest(Static.class) // Staticメソッドをモックするクラスを指定(,で複数指定可) | |
public class MyTestCase extends TestCase { |
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
PowerMockito.mockStatic(StaticHogeClass.class); | |
Mockito.when(StaticHogeClass.apply()).thenThrow(new Throwable("Hello PowerMock")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment