How to write a test for a function that uses AndroidSchedulers.mainThread(); from RxAndroid

414 views Asked by At

I am getting a NullPointerException in the test method shouldFetchMachineSettings(). By debugging I realised that the control didn't go to the success callback of the observable and the intent was not launched. Hence the exception. I realised that the problem was with using AndroidSchedulers.mainThread(); but was not able to find a solution for it.

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
            }
        };
    }
}

The call from MachineDetailsFragment.java

if (item.getItemId() == R.id.action_machine_settings) {
    setupProgressDialog();
    MachineSettings.fetch(getActivity(), machineService, progressDialog);
}

Test in 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()));
1

There are 1 answers

0
Jeroen On

Modify your class so it doesn't use static methods and create a constructor that accepts the two Schedulers you want to use. In your test you can then use different schedulers like TestScheduler or Schedulers.immediate().

As a general rule of thumb: static methods that call other static methods (or use global state, singletons) are very hard to test.