TestScheduler在测试特定用例时不工作

时间:2018-03-19 07:55:34

标签: android testing junit kotlin rx-java2

我正在测试单个用例。但测试观察者不会在测试环境中发出任何东西。

  • 即使我将subscribeOn()Schedulers.newThread()更改为 TestScheduler()仍然测试失败。我很困惑。而且我不知道是什么 我在这里做错了吗?
  • 如果我删除subscribeOnobserveOn方法,则测试成功通过。然后 TestScheduler()的实际用途是什么?

以下是UseCase.kt文件

abstract class UseCase<T>(val postExecutionThread: PostExecutionThread) {

    abstract fun buildUseCaseBuilder(): Observable<T>

    /**
     * execute method for observables
     */
    open fun execute(): Observable<T> {
        return buildUseCaseBuilder()
                .subscribeOn(Schedulers.newThread())
                .observeOn(postExecutionThread.getScheduler())
    }
}

跟随上述测试类的测试类

class UseCaseTest {

    val postExecutionThread=Mockito.mock(PostExecutionThread::class.java)
    val result: String="test"
    val testObserver=TestObserver<String>()

    @Before
    fun setUp() {
        _when(postExecutionThread.getScheduler()).thenReturn(TestScheduler())
    }

    @Test
    fun `test execute method of use case`() {
        println(" thread type ${postExecutionThread.getScheduler()}")

        //test fail
        //java.lang.AssertionError: Value count differs; Expected: 1 [test], Actual: 0 [] (latch = 1, values = 0, errors = 0, completions = 0)
        TestUseCase(postExecutionThread).execute().test()
                .assertResult(result)

        //subscriber not print any thing
        TestUseCase(postExecutionThread).execute().subscribe{
            println("called $it")
        }
    }

    inner class TestUseCase(postExecutionThread: PostExecutionThread?) : UseCase<String>(postExecutionThread!!) {
        override fun buildUseCaseBuilder(): Observable<String> {
            return Observable.just(result)
        }
    }
}

1 个答案:

答案 0 :(得分:2)

问题是,您的测试是在除您使用subscribeOn()observeOn()指定的线程之外运行的。你想要的是使测试和测试的代码在同一个线程上同步运行,无论subscribeOn() / observeOn()中指定了什么。

您可以使用@Rule之类的自定义this one来实现这一目标:

/**
 * This rule registers Handlers for RxJava and RxAndroid to ensure that subscriptions
 * always subscribeOn and observeOn Schedulers.trampoline().
 * Warning, this rule will reset RxAndroidPlugins and RxJavaPlugins before and after each test so
 * if the application code uses RxJava plugins this may affect the behaviour of the testing method.
 */
public class RxSchedulersOverrideRule implements TestRule {

    private final Function<Callable<Scheduler>, Scheduler> mRxAndroidSchedulersHook =
            new Function<Callable<Scheduler>, Scheduler>() {
                @Override
                public Scheduler apply(@NonNull Callable<Scheduler> schedulerCallable)
                        throws Exception {
                    return getScheduler();
                }
            };

    private final Function<Scheduler, Scheduler> mRxJavaImmediateScheduler =
            new Function<Scheduler, Scheduler>() {
                @Override
                public Scheduler apply(@NonNull Scheduler scheduler) throws Exception {
                    return getScheduler();
                }
            };

    @Override
    public Statement apply(final Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                RxAndroidPlugins.reset();
                RxAndroidPlugins.setInitMainThreadSchedulerHandler(mRxAndroidSchedulersHook);

                RxJavaPlugins.reset();
                RxJavaPlugins.setIoSchedulerHandler(mRxJavaImmediateScheduler);
                RxJavaPlugins.setNewThreadSchedulerHandler(mRxJavaImmediateScheduler);

                base.evaluate();

                RxAndroidPlugins.reset();
                RxJavaPlugins.reset();
            }
        };
    }

    public Scheduler getScheduler() {
        return Schedulers.trampoline();
    }

}

使用此代码,您可以拦截所有调度程序,并使所有内容在trampoline()调度程序上运行。

现在只需在您的测试中添加以下@Rule


    public class MyTestClass {

        @Rule
        public final RxSchedulersOverrideRule mOverrideSchedulersRule = new RxSchedulersOverrideRule();

        @Test
        public void someTest() {
            ...
        }
    }