本文仅在记录自己遇到过的异常情况
1、java.lang.Exception: Test class should have exactly one public zero-argument constructor
测试类应该只有一个公共零参数构造函数
解决:
public ExampleInstrumentedTest(Class<MainActivity> activityClass) { super(activityClass, true); }
改成
public ExampleInstrumentedTest() { super(MainActivity.class); }
2、java.lang.ClassCastException:cn.com.poul.multiprogress.FirstActivity cannot be cast to cn.com.poul.multiprogress.MainActivity
以ActivityTestRule为例:(对应类别测试依赖对应包,可查看官方文档)
原因:launchActivity(intent),查看官方文档可知Launches the Activity under test.(启动受测试的活动),由于自己没仔细看文档造成
解决:
@Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
eventBusActivity = mActivityRule.getActivity(); Intent intent = new Intent(eventBusActivity, FirstActivity.class);
mActivity = mActivityRule.launchActivity(intent);
改成
@Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
eventBusActivity = mActivityRule.getActivity(); Intent intent = new Intent(eventBusActivity, MainActivity.class);
mActivity = mActivityRule.launchActivity(intent);
3、。。。。