我已经成功创建了一个自定义espresso测试运行器,如下所示:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
[self performSelector:@selector(jsq_updateCollectionViewInsets)];
#pragma clang diagnostic pop
我的应用程序的网络层是用rxjava2编写的,所以我需要一种方法来将espresso调度与rxjava链接起来,所以我选择使用Rx2Idler
但我遇到的问题是,当我运行测试时,我收到以下错误:
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);
}
}
这是我的实际测试,如果它有任何重要性。测试失败,但是如果我删除了调度程序覆盖,则它适用于sleep:
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)
另请注意,我的所有rxjava调用如下所示:
@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());
}
}
更新:我想知道它的firebase网络调用是否同步空闲。这些调用没有使用rxjava,所以它们不能由Rx2Idler处理吗?
答案 0 :(得分:0)
我尝试了各种选项,例如在Test Runner类的template <typename T>
struct S
{
private:
T t;
void print_helper (std::true_type) // T is std::string
{ std::cout << "string: " << t << std::endl; }
void print_helper (std::false_type) // T isn't std::string
{ std::cout << t << std::endl; }
public:
S (T const & t0) : t{t0}
{ }
void print ()
{ print_helper(std::is_same<T, std::string>{}); }
};
中设置RxJava插件,或在所有资源上注册ResourceCallback。
但是对我有用的解决方案是在onCreate()
请在此处检查代码
super.onStart()