我是Android的新手,我正在与Robolectric一起进行单元测试.
我正面临以下问题.
我有一个要测试的活动.
MainActivity.java
@H_403_11@public class MainActivity extends ActionBaractivity implements NavigationDrawerFragment.NavigationDrawerCallbacks { private NavigationDrawerFragment mNavigationDrawerFragment; @OverrIDe protected voID onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentByID(R.ID.navigation_drawer); mNavigationDrawerFragment.setUp( R.ID.navigation_drawer, (DrawerLayout) findVIEwByID(R.ID.drawer_layout)); } @OverrIDe public voID onNavigationDrawerItemSelected (int position) { ... }}
这是测试类:
@H_403_11@@RunWith(RobolectricGradleTestRunner.class)@Config(constants = BuildConfig.class)public class MainActivityTests { private ActivityController<MainActivity> controller; private MainActivity activity; private MainActivity spy; @Test public voID onCreate_shouldStartNavigationDrawerFragment () { controller = Robolectric.buildActivity(MainActivity.class); activity = controller.get(); assertthat(activity).isNotNull(); spy = spy(activity); spy.onCreate(null); verify(spy).onCreate(null); }}
但是我收到以下异常:
java.lang.IllegalStateException:在spy.onCreate(null)行的onCreate()之前,ActivitIEs无法使用系统服务.
我已经搜寻了几个小时,并且尝试了几种变通方法(盲目),但均未成功.可以请任何人指导我吗?
解决方法:
这就是我的诀窍.我先使用attach()进行活动监视.经过Robolectric 3.0测试
@H_403_11@private MainActivity spyActivity;@Beforepublic voID setUp(){ MainActivity activity = Robolectric.buildActivity(MainActivity.class).attach().get(); spyActivity = spy(activity); spyActivity.onCreate(null);}总结
以上是内存溢出为你收集整理的java-使用Robolectric时如何监视活动全部内容,希望文章能够帮你解决java-使用Robolectric时如何监视活动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)