Skip to content

Instantly share code, notes, and snippets.

@YukiMatsumura
Created April 28, 2015 14:17
Show Gist options
  • Save YukiMatsumura/60ad4a2de5f84dd77982 to your computer and use it in GitHub Desktop.
Save YukiMatsumura/60ad4a2de5f84dd77982 to your computer and use it in GitHub Desktop.
AndroidStudio + PowerMock でstaticメソッドをmockする ref: http://qiita.com/Yuki_312/items/80e0cda79731eb56c769
// 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'
testOptions {
unitTests.returnDefaultValues = true
}
@RunWith(PowerMockRunner.class)
public class MyTestCase extends TestCase {
@RunWith(PowerMockRunner.class)
@PrepareForTest(Static.class) // Staticメソッドをモックするクラスを指定(,で複数指定可)
public class MyTestCase extends TestCase {
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