使用改造和 Rx 进行单元测试 android

Unit Testing with retrofit and Rx android

我是单元测试的新手,我正在使用 rx android 进行单元测试改造。我有一个 observable,它从 api 获取访问令牌,我使用它通过改造来发送请求。由于它,我得到空指针异常这是我的代码:

@RunWith(MockitoJUnitRunner.class)
public class AuthenticationTokenGetterTest {
    @Mock
    AuthenticatorInterface authenticatorservice;
    @InjectMocks
    AuthenticationTokenGetter tokengetter;

    @Test
    public void testtokkengetter() {
        when(authenticatorservice.servicecall(anyString(), anyString())).thenReturn(
                Observable.just("44fffffggggggg"));

        Observable<String> obs = tokengetter.getToken();
        TestSubscriber<String> testsubscriber = new TestSubscriber<>();
        obs.subscribe(testsubscriber);
        testsubscriber.assertNoErrors(); // Here I get exception
        List<String> value = testsubscriber.getOnNextEvents();
    }

}

但我一直在 java.lang.NullPointerException。 我正在测试的可观察代码是:

@CheckResult
    public Observable<String> getToken() {
        return service.servicecall(key, code)
                .subscribeOn(Schedulers.newThread())
                .doOnNext(new Action1<String>() {
                    public void call(String token) {
                        savedToken = token;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
    }

我的错误是:

java.lang.AssertionError: Unexpected onError events: 1

    at rx.observers.TestSubscriber.assertNoErrors(TestSubscriber.java:308)
    at AuthenticationTokenGetterTest.testtokkengetter(AuthenticationTokenGetterTest.java:47)
    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.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    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:147)
Caused by: java.lang.NullPointerException
    at rx.android.schedulers.LooperScheduler$HandlerWorker.schedule(LooperScheduler.java:77)
    at rx.android.schedulers.LooperScheduler$HandlerWorker.schedule(LooperScheduler.java:91)
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.schedule(OperatorObserveOn.java:190)
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.request(OperatorObserveOn.java:147)
    at rx.Subscriber.setProducer(Subscriber.java:209)
    at rx.Subscriber.setProducer(Subscriber.java:205)
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.init(OperatorObserveOn.java:141)
    at rx.internal.operators.OperatorObserveOn.call(OperatorObserveOn.java:75)
    at rx.internal.operators.OperatorObserveOn.call(OperatorObserveOn.java:40)
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:46)
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
    at rx.Observable.subscribe(Observable.java:8759)
    at rx.Observable.subscribe(Observable.java:8726)
.AuthenticationTokenGetterTest.testtokkengetter(AuthenticationTokenGetterTest.java:45)

您正在使用 AndroidSchedulers.mainThread(),它依赖于 Android Looper class,这就是空指针的原因。不要创建使用多线程的单元测试,在同一线程上执行所有内容!

您可以通过执行调度程序注入来解决此问题。您的 AuthenticationTokenGetter class 应该通过在构造函数中传递的 Scheduler 引用获取 mainThreadScheduler 实例,因此在您的正常代码中,您应该使用 mainThreadScheduler 创建对象并在测试期间创建您的对象具有 Scheduler 同步执行所有内容的实现。

您还可以使用 RxJava/RxAndroidSchedulersHook 覆盖调度程序。

@编辑 一些解释如何 inject/ovveride 调度程序的文章:

https://medium.com/@peter.tackage/overriding-rxandroid-schedulers-in-rxjava-2-5561b3d14212

https://medium.com/@peter.tackage/an-alternative-to-rxandroidplugins-and-rxjavaplugins-scheduler-injection-9831bbc3dfaf

这两种解决方案都有其优点和缺点,尽管这些文章针对的是 RxJava2,但这种方法对 RxJava1 仍然有效(但是调度程序 hooks/plugins 在 Rx2 中的工作方式略有不同)

我使用这个技术:

  1. BaseSchedulerProvider(父亲)
  2. ImmediateSchedulerProvider(测试)
  3. SchedulerProvider(应用程序)

基础调度程序提供者:

public interface BaseSchedulerProvider {

    @NonNull
    Scheduler computation();

    @NonNull
    Scheduler io();

    @NonNull
    Scheduler ui();
}

我用于测试的ImmediateSchedulerProvider:

public class ImmediateSchedulerProvider implements BaseSchedulerProvider {

    @NonNull
    @Override
    public Scheduler computation() {
        return Schedulers.immediate();
    }

    @NonNull
    @Override
    public Scheduler io() {
        return Schedulers.immediate();
    }

    @NonNull
    @Override
    public Scheduler ui() {
        return Schedulers.immediate();
    }
}

我在 Presenter 中使用的 SchedulerProvider

public class SchedulerProvider implements BaseSchedulerProvider {

    // Prevent direct instantiation.
    public SchedulerProvider() {
    }

    @Override
    @NonNull
    public Scheduler computation() {
        return Schedulers.computation();
    }

    @Override
    @NonNull
    public Scheduler io() {
        return Schedulers.io();
    }

    @Override
    @NonNull
    public Scheduler ui() {
        return AndroidSchedulers.mainThread();
    }
}

在我的 PresenterTest 中,我这样设置:

public class TopicPresenterTest {

            @Mock
            private RemoteDataSource mRemoteDataSource;

            @Mock
            private TopicContract.View mView;

            private BaseSchedulerProvider mSchedulerProvider;

            TopicPresenter mPresenter;

            List<Topics> mList;

            @Before
            public void setup() {
            MockitoAnnotations.initMocks(this);

                    Topics topics = new Topics(1, "Discern The Beach");
                    Topics topicsTwo = new Topics(2, "Discern The Football Player");
                    mList = new ArrayList<>();
                    mList.add(topics);
                    mList.add(topicsTwo);
//ADD IMMEDIATESCHEDULERPROVIDER !!!!!!!!!!!!!!!
                    mSchedulerProvider = new 
                    ImmediateSchedulerProvider();

                    mPresenter = new TopicPresenter(mRemoteDataSource, mView, mSchedulerProvider);

            }

            @Test
            public void fetchData() {

                when(mRemoteDataSource.getTopicsRx())
                        .thenReturn(rx.Observable.just(mList));

                mThemePresenter.fetch();

                InOrder inOrder = Mockito.inOrder(mView);
                inOrder.verify(mView).setLoadingIndicator(false);
                inOrder.verify(mView).showTopics(mList);

            }

}

在我的演示者中

public class TopicPresenter {

        @NonNull
        private BaseSchedulerProvider mSchedulerProvider;

        public TopicPresenter(@NonNull RemoteDataSource remoteDataSource, @NonNull TopicContract.View view) {
                    this.mRemoteDataSource = checkNotNull(remoteDataSource, "remoteDataSource");
                    this.mView = checkNotNull(view, "view cannot be null!");
                    this.mSchedulerProvider = new SchedulerProvider();
 //ADD COMPOSITESUBSCRITPTION !!!!!!     
                    mSubscriptions = new CompositeSubscription();

                    mView.setPresenter(this);
        }
}

你可以查看my complete example in GitHub and this article.