gpt4 book ai didi

android - 带有 RxJava2 的 espresso 自定义 TestRunner - 使用 Rx2Idler 获取空指针异常

转载 作者:行者123 更新时间:2023-12-01 04:45:45 29 4
gpt4 key购买 nike

我已经成功创建了一个自定义的 espresso 测试运行器,如下所示:

public class PomeloTestRunner extends AndroidJUnitRunner {

@Override
public void onCreate(Bundle arguments) {
RxJavaPlugins.setInitComputationSchedulerHandler(
Rx2Idler.create("RxJava 2.x Computation Scheduler"));

RxJavaPlugins.setInitIoSchedulerHandler(Rx2Idler.create("RxJava 2.x Computation Scheduler"));
RxJavaPlugins.setInitNewThreadSchedulerHandler(Rx2Idler.create("RxJava 2.x Computation Scheduler"));
RxJavaPlugins.setInitSingleSchedulerHandler(Rx2Idler.create("RxJava 2.x Computation Scheduler"));

super.onCreate(arguments);
}
}

我的应用程序的网络层是用 rxjava2 编写的,所以我需要一种将 espresso 调度与 rxjava 链接的方法,所以我选择使用 Rx2Idler

但我遇到的问题是,当我运行测试时,出现以下错误:
FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: com.mobile.pomelo.labs, PID: 11611
java.lang.NullPointerException: Attempt to invoke interface method 'void android.support.test.espresso.IdlingResource$ResourceCallback.onTransitionToIdle()' on a null object reference
at com.squareup.rx2.idler.DelegatingIdlingResourceScheduler.stopWork(DelegatingIdlingResourceScheduler.java:96)
at com.squareup.rx2.idler.DelegatingIdlingResourceScheduler$ScheduledWork.run(DelegatingIdlingResourceScheduler.java:142)
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:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

如果它有任何重要性,这是我的实际测试。测试失败,但如果我删除调度程序覆盖它与 sleep 一起工作:
    @RunWith(AndroidJUnit4.class)

public class AuthenticationActivityTest {

@Before
public void setUp() throws Exception {
mIntentsTestRule.getActivity().sharedPrefRepo.clearAll();
}


@Rule
public IntentsTestRule<AuthenticationActivity> mIntentsTestRule =
new IntentsTestRule<AuthenticationActivity>(AuthenticationActivity.class) {
@Override
protected Intent getActivityIntent() {
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
Intent result = new Intent(targetContext, AuthenticationActivity.class);
Bundle b = new Bundle();
b.putSerializable("action", AuthenticationActivity.LoginActions.LOGIN);

result.putExtras(b);
return result;
}
};



@Test
public void signupWithFacebook() {

final Intent resultData = new Intent();
resultData.putExtra("com.facebook.LoginFragment:Result", LoginClientCreator.createResult());
intending(hasComponent(FacebookActivity.class.getName()))
.respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData));

// click signup fb button
onView(withId(R.id.fb_login_button))
.perform(click());

// check facebook intent was hit
intended(hasComponent(FacebookActivity.class.getName()), times(1));

try {
Thread.sleep(14000); // i dont want this to sleep, i would rather the espresso idle sync work with rxjava2
} catch (InterruptedException e) {
e.printStackTrace();
}

ViewInteraction appCompatTextView = onView(allOf(withText("COLLECTIONS"), isDisplayed()));
appCompatTextView.perform(click());
}
}

另请注意,我所有的 rxjava 调用如下所示:
public void execute(@NonNull Observer UseCaseSubscriber){
this.buildUseCaseObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(UseCaseSubscriber);
}

更新:我想知道它的firebase网络调用是否没有空闲同步。这些调用没有使用 rxjava,所以它们不能由 Rx2Idler 处理吗?

最佳答案

我尝试了各种选项,例如在 onCreate() 中设置 RxJava 插件。 Test Runner 类,或在所有资源上注册 ResourceCallback。

但是对我有用的解决方案只是在 super.onStart() 之后设置 RxJavaPlugins

请在此处检查代码:

public class AppTestRunner extends AndroidJUnitRunner {

@Override
public void onStart() {
super.onStart();

setupRxJavaPlugins();
}

private void setupRxJavaPlugins() {
// rx-java 2
RxJavaPlugins.setInitComputationSchedulerHandler(Rx2Idler.create("RxJava 2.x Computation Scheduler"));

RxJavaPlugins.setInitIoSchedulerHandler(Rx2Idler.create("RxJava 2.x IO Scheduler"));

RxJavaPlugins.setInitNewThreadSchedulerHandler(Rx2Idler.create("RxJava 2.x NewThread Scheduler"));

RxJavaPlugins.setInitSingleSchedulerHandler(Rx2Idler.create("RxJava 2.x Single Scheduler"));

// rx-java 1
rx.plugins.RxJavaPlugins.getInstance().registerSchedulersHook(RxIdler.hooks());
}
}

关于android - 带有 RxJava2 的 espresso 自定义 TestRunner - 使用 Rx2Idler 获取空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769919/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com