Kai*_*Kai 4 java testing android android-espresso conductor
我正在为使用Conductor编写的应用程序编写Espresso测试。我想为每个测试指定要启动哪个控制器,这样我就不需要Espresso从每个活动的开始Activity中单击应用程序。由于只有一个活动,并没有太多的SO或google一下指挥我能找到最接近的是这个问题吗?还是不可能?
我尝试将路由器设为静态并添加吸气剂,以尝试设置特定的根进行测试,但均未成功。
android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
在MainActivity中:
public static Router getRouter() {
return router;
}
Run Code Online (Sandbox Code Playgroud)
在仪器测试中:
@Rule
public ActivityTestRule<MainActivity> testRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void setUp() {
Router router = testRule.getActivity().getRouter();
router.setRoot(RouterTransaction.with(new ControllerIwantToTest()));
}
@Test
public void titleIsDisplayed() {
onView(withText("My Controllers Title")).check(matches(isDisplayed()));
}
Run Code Online (Sandbox Code Playgroud)
如果其他任何人遇到相同的问题,我可以通过执行以下操作解决问题:
@RunWith(AndroidJUnit4.class)
public class NoBundleControllerEspressoTest {
private Router router;
@Rule
public ActivityTestRule<NoBundleConductorActivity> testRule = new ActivityTestRule<>(NoBundleConductorActivity.class);
@Before
public void setUp() {
Activity activity = testRule.getActivity();
activity.runOnUiThread(() -> {
router = testRule.getActivity().getRouter();
router.setRoot(RouterTransaction.with(new NoBundleController()));
});
}
@Test
public void titleIsDisplayed() {
onView(withText("Super Awesome Title")).check(matches(isDisplayed()));
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您的控制器Bundle像我们大多数情况一样在其构造器中使用a :
@RunWith(AndroidJUnit4.class)
public class BundleControllerEspressoTest {
private Router router;
private ControllerBundleData controllerData;
@Rule
public ActivityTestRule<BundleConductorActivity> testRule = new ActivityTestRule<>(BundleConductorActivity.class);
@Before
public void setUp() {
controllerData = new ControllerBundleData();
Bundle bundle = new Bundle();
bundle.putSerializable(YOUR_BUNDLE_KEY, controllerData);
Activity activity = testRule.getActivity();
activity.runOnUiThread(() -> {
router = testRule.getActivity().getRouter();
router.setRoot(RouterTransaction.with(new BundleController(bundle)));
});
}
@Test
public void titleIsDisplayed() {
onView(withText("Super Awesome Title")).check(matches(isDisplayed()));
}
}
Run Code Online (Sandbox Code Playgroud)