NullPointerException
テストメソッドで を取得していshouldFetchMachineSettings()
ます。デバッグすることで、コントロールがオブザーバブルの成功コールバックに行かず、インテントが起動されていないことに気付きました。したがって、例外です。問題は使用にあることに気付きましたがAndroidSchedulers.mainThread();
、解決策を見つけることができませんでした。
MachineSettings.java
public class MachineSettings {
public static void fetch(final FragmentActivity activity,
MachineService machineService,
final ProgressDialog progressDialog) {
Observable zippedCalls = Observable.zip(
machineService.getSettings(),
machineService.getInfoAsObservable(),
machineService.getSupportedLanguages(),
new Func3() {
@Override
public Object call(final Object response1,
final Object response2,
final Object response3) {
return new HashMap<String, Object>() {{
put(MACHINE_SETTINGS, response1);
put(MACHINE_SETTINGS_INFO, response2);
put(MACHINE_SETTINGS_LANGUAGES, response3);
}};
}
});
zippedCalls.subscribeOn(getNewThread())
.observeOn(getAndroidSchedulerMainThread())
.subscribe(onSuccess(activity, progressDialog)
, onError(activity, progressDialog));
}
public static Scheduler getAndroidSchedulerMainThread() {
return AndroidSchedulers.mainThread();
}
public static Scheduler getNewThread() {
return Schedulers.newThread();
}
private static Action1 onSuccess(final FragmentActivity activity,
final ProgressDialog progressDialog) {
return new Action1() {
@Override
public void call(Object o) {
Intent intent = new Intent(activity, MachineSettingsActivity_.class);
// do some action using object o
activity.startActivity(intent);
}
};
}
private static Action1<Throwable> onError(final FragmentActivity activity,
final ProgressDialog progressDialog) {
return new Action1<Throwable>() {
@Override
public void call(Throwable error) {
// do some action
}
};
}
}
MachineDetailsFragment.java からの呼び出し
if (item.getItemId() == R.id.action_machine_settings) {
setupProgressDialog();
MachineSettings.fetch(getActivity(), machineService, progressDialog);
}
MachineDetailsFragmentTest.java でのテスト
@Test
public void shouldFetchMachineSettings() {
MenuItem mockMenuItem = mock(MenuItem.class);
when(mockMenuItem.getItemId()).thenReturn(R.id.action_machine_settings);
Map<String, Integer> settings = new HashMap() {{put("privacy", 0);}};
Map<String, Integer> languages = new HashMap() {{put("index_0", 2);}} ;
Map<String, String> info = new HashMap() {{put("version", "1");}};
Observable<Map<String, Integer>> mockSupportedLanguages = Observable.just(languages);
Observable<Map<String, Integer>> mockSettings = Observable.just(settings);
Observable<Map<String, String>> mockInfo = Observable.just(info);
when(machineService.getSupportedLanguages()).thenReturn(mockSupportedLanguages);
when(machineService.getSettings()).thenReturn(mockSettings);
when(machineService.getInfoAsObservable()).thenReturn(mockInfo);
machineDetailFragment.onOptionsItemSelected(mockMenuItem);
Intent intent = Shadows.shadowOf(activity).peekNextStartedActivity();
assertThat(intent.getComponent().getClassName(),
is(MachineSettingsActivity_.class.getCanonicalName()));