我正在了解RxJava。我开始在我的一个个人应用程序中使用它,并希望单元测试代码,但有一些困难,并希望得到一些帮助。
情景很简单。
UserInfo
个对象UserInfo
对象不为null,则返回true
,否则返回false
。对于上面的场景,我的RxJava代码看起来像这样
public LiveData<Boolean> doesUserExists(String userName) {
UserExistsObserver observer= new UserExistsObserver ();
getUserInfo(userName).subscribeWith(subscriber);
disposable.add(observer);
return userExists;
}
public Observable<Boolean> getUserInfo(String userName) {
return repository.getUserInfo(userName)
.flatMap(new Function<UserInfo, Observable<Boolean>>() {
@Override
public Observable<Boolean> apply(UserInfo userInfo) throws Exception {
return Observable.just(userInfo != null);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
所以我想编写一个简单的单元测试来检查getUserInfo()是否返回正确的布尔值。以下是我的单元测试。
@Test
public void getUserInfo_returns_true(){
UserInfo userInfo = new UserInfo(); //Dummy data - non null userInfo object
when(repository.getUserInfo("username")).thenReturn(Observable.just(userInfo));
TestObserver<Boolean> testObserver = new TestObserver<>();
//the flatMap operator should return true since userInfo is not null
viewModel.getUserInfo("username").subscribeWith(testObserver);
testObserver.assertValue(true);
}
以下是我的日志
java.lang.AssertionError: Expected: true (class: Boolean), Actual: [] (latch = 1, values = 0, errors = 0, completions = 0)
at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:163)
at io.reactivex.observers.BaseTestConsumer.assertValue(BaseTestConsumer.java:328)
at com.ik.githubbrowser.search_user.SearchUserViewModelTest.getUserInfo_returns_true(SearchUserViewModelTest.java:51)
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: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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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)
java.lang.NullPointerException
at io.reactivex.android.schedulers.HandlerScheduler$HandlerWorker.schedule(HandlerScheduler.java:70)
at io.reactivex.Scheduler$Worker.schedule(Scheduler.java:272)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.schedule(ObservableObserveOn.java:161)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.onNext(ObservableObserveOn.java:119)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:58)
at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarDisposable.run(ObservableScalarXMap.java:248)
at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarXMapObservable.subscribeActual(ObservableScalarXMap.java:164)
at io.reactivex.Observable.subscribe(Observable.java:10903)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "RxCachedThreadScheduler-1" java.lang.NullPointerException
at io.reactivex.android.schedulers.HandlerScheduler$HandlerWorker.schedule(HandlerScheduler.java:70)
at io.reactivex.Scheduler$Worker.schedule(Scheduler.java:272)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.schedule(ObservableObserveOn.java:161)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.onNext(ObservableObserveOn.java:119)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:58)
at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarDisposable.run(ObservableScalarXMap.java:248)
at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarXMapObservable.subscribeActual(ObservableScalarXMap.java:164)
at io.reactivex.Observable.subscribe(Observable.java:10903)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code -1
答案 0 :(得分:10)
您的方法必须通过2个不同的线程来生成结果(由于subscribeOn
和observeOn
调用)。这意味着观察者需要时间来实际产生结果。在检查TestObserver.awaitTerminalEvent()
之前使用assertValue
以确保observable实际产生了值。
或者,在测试代码时,您应该使用不同的调度程序,因为Android调度程序可能需要额外的代码才能在测试环境中正常运行。
答案 1 :(得分:8)
正如@kiskae建议我必须更换调度程序。我替换了subscribeOn
和observeOn
调度程序。我们的想法是在同一个线程上执行操作,从而使其同步。由于此单元测试在JVM上运行,因此JVM将无法访问作为调度程序传递给AndroidSchedulers.mainThread()
的Android特定observeOn
。所以我们在RxAndroidPlugins
类的帮助下替换了这个调度程序。我们也使用subscribeOn
类替换传递给RxJavaPlugins
的调度程序。
欲了解更多信息,请阅读this中等帖子。
以下是我的工作代码。
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.concurrent.Callable;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.observers.TestObserver;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class SearchUserViewModelTest {
private RepositoryImpl repository;
private SearchUserViewModel viewModel;
@BeforeClass
public static void before(){
RxAndroidPlugins.reset();
RxJavaPlugins.reset();
RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(@NonNull Scheduler scheduler) throws Exception {
return Schedulers.trampoline();
}
});
RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {
@Override
public Scheduler apply(@NonNull Callable<Scheduler> schedulerCallable) throws Exception {
return Schedulers.trampoline();
}
});
}
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
repository = mock(RepositoryImpl.class);
viewModel = new SearchUserViewModel(repository);
}
@Test
public void getUserInfo_returns_true(){
UserInfo userInfo = new UserInfo();
userInfo.setName("");
when(repository.getUserInfo(anyString())).thenReturn(Observable.just(userInfo));
TestObserver<Boolean> testObserver = new TestObserver<>();
viewModel.getUserInfo(anyString()).subscribe(testObserver);
testObserver.assertValue(true);
}
@AfterClass
public static void after(){
RxAndroidPlugins.reset();
RxJavaPlugins.reset();
}
}
答案 2 :(得分:1)
通过定义新的测试规则,您可以保持测试类的干净,并再次在其他测试类中使用此规则
public class RxSchedulerRule implements TestRule {
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable
-> TrampolineScheduler.instance());
RxJavaPlugins.setIoSchedulerHandler(scheduler
-> TrampolineScheduler.instance());
RxJavaPlugins.setComputationSchedulerHandler(scheduler ->
TrampolineScheduler.instance());
try{
base.evaluate();
}finally {
RxAndroidPlugins.reset();
RxJavaPlugins.reset();
}
}
};
}
}
在您的测试课程中
@Rule
public RxSchedulerRule rxSchedulerRule=new RxSchedulerRule();