以前のリビジョンの文書です
オブジェクトAが××の状態の場合, ○○の振る舞いをする… などのテストに利用する。
オブジェクトAの代わりにモックを使用することにより、オブジェクトAを特定の状態にセットアップ
する処理を簡略化することができる。
build.gradleのdependenciesに下記の3行を追加。Mockito本体の他にdex化のライブラリが必要な模様。
androidTestCompile 'org.mockito:mockito-core:1.+' androidTestCompile 'com.google.dexmaker:dexmaker:1.+' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
// Mockitoクラスはstaticインポートすることを推奨
import static org.mockito.Mockito.*;
...
@test
public void something() throws Exception {
// mockオブジェクトを生成
List mockedList = mock(List.class);
// スタブとして"first"を返すよう設定
when(mockedList.get(0)).thenReturn("first");
String str = mockedList.get(0).toString();
assertEquals("first", str);
}