Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava Schedules.io() NullPointerException in Unit Tests

I am attempting to test this class in a standard JUnit test, however, I am getting hung up on a NullPointerException with Schedulers.io(). Can Schedulers.io() be mocked?

This is an Android application and I am attempting to provide full coverage over the code using travis-ci for continuous integration and coveralls.io for reporting coverage.

Class to be Tested:

public class GetLiveStreamsList extends UseCase {

    private final String filename;
    private final ContentRepository contentRepository;

    public GetLiveStreamsList( final String filename, final ContentRepository contentRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread ) {
        super( threadExecutor, postExecutionThread );

        this.filename = filename;
        this.contentRepository = contentRepository;

    }

    @Override
    protected Observable buildUseCaseObservable() {

        Action1<List<LiveStreamInfo>> onNextAction = new Action1<List<LiveStreamInfo>>() {

            @Override
            public void call( List<LiveStreamInfo> liveStreamInfos ) {

                try {
                    Thread.sleep( 5000 );
                } catch( InterruptedException e ) { }

            }

        };

        return this.contentRepository.liveStreamInfos( this.filename )
                .repeat( Schedulers.io() )
                .doOnNext( onNextAction );
    }

}

Test Class:

public class GetLiveStreamsListTest {

    private static final String FAKE_FILENAME = "fake filename";

    private GetLiveStreamsList getLiveStreamsList;

    @Mock
    private ThreadExecutor mockThreadExecutor;

    @Mock
    private PostExecutionThread mockPostExecutionThread;

    @Mock
    private ContentRepository mockContentRepository;

    @Before
    public void setUp() {

        MockitoAnnotations.initMocks( this );
        getLiveStreamsList = new GetLiveStreamsList( FAKE_FILENAME, mockContentRepository, mockThreadExecutor, mockPostExecutionThread );

    }

    @Test
    public void testGetLiveStreamsListUseCaseObservableHappyCase() {

        getLiveStreamsList.buildUseCaseObservable();

        verify( mockContentRepository ).liveStreamInfos( FAKE_FILENAME );
        verifyNoMoreInteractions( mockContentRepository );
        verifyZeroInteractions( mockThreadExecutor );
        verifyZeroInteractions( mockPostExecutionThread );

    }

}

Here is the stacktrace:

java.lang.NullPointerException
    at org.mythtv.android.domain.interactor.GetLiveStreamsList.buildUseCaseObservable(GetLiveStreamsList.java:47)
    at org.mythtv.android.domain.interactor.GetLiveStreamsListTest.testGetLiveStreamsListUseCaseObservableHappyCase(GetLiveStreamsListTest.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


Process finished with exit code 255
like image 684
dmfrey Avatar asked Apr 12 '16 20:04

dmfrey


1 Answers

You forgot to set the return value of contentRepository.liveStreamInfos. It's null by default.

You need to set the return value like this:

when(contentRepository.liveStreamInfos()).thenReturn(...);
like image 99
zsxwing Avatar answered Nov 06 '22 04:11

zsxwing