Skip to content
Francisco Gonzalez edited this page Mar 2, 2017 · 3 revisions
  • How can I test large amount of data from a mocked request like a JSON file?

    Have a view into /data/src/test/ MockResponseDispatcher class and see how it is handled.

    new File(classLoader.getResource(fileName).getFile())
  • How can I chain and make parallel requests with RxJava and Retrofit?

    Look straight into the /data/.../interactor/ GetPostList class and you'll find a particular strategy that uses groupBy and flatMap [RxJava Operators][16] plus the number of available processors in the device, to achieve the parallelism execution.

  • How can I take control of Dagger 2 modules and implement IdlingResource in Espresso tests?

    Pay a special attention into the /app/src/androidTest/.../feature/ EspressoTest class.

    Remember you need to attach EspressoTestRunner to your Run/Debug Configurations in Android Studio.

  • How can I override default Schedulers?

    Have a look into the /data/src/test/ RxJavaTestRunner class and you will see Hooks implementation and how to use it for RxJava v1.2

    RxJavaHooks.reset();
    RxJavaHooks.setOnIOScheduler(scheduler -> Schedulers.immediate());
    RxJavaHooks.setOnNewThreadScheduler(scheduler -> Schedulers.immediate());
  • How can I deal with Threads in RxJava (Unit Tests specific)?

    The quick answer is by using TestScheduler class.

    Although in order to use it properly, you must implement your code with the ability to pass the Scheduler you want as a parameter, normally in the constructor of your class.

    public interface ThreadExecutor {
        Scheduler getScheduler();
    }
    
    ...
    
    public class ClassToBeTested {
        public ClassToBeTested(ThreadExecutor executor) {
            // any implementation that uses ThreadExecutor#getScheduler()
        }
    }
    
    ...
    
    ThreadExecutor mockThreadExecutor = mock(ThreadExecutor.class);
    ClassToBeTested classToBeTested = new ClassToBeTested(mockThreadExecutor);
    
    TestScheduler testScheduler = new TestScheduler();
    given(mockThreadExecutor.getScheduler()).willReturn(testScheduler);
    
    // An execution that uses that Scheduler
    classToBeTested.execute();
    testScheduler.triggerActions();

Have a question? Just open an issue!

Clone this wiki locally