Android - AssertionFailedError on startActivity method in ActivityUnitTestCase test class

微笑、不失礼 提交于 2019-12-03 01:29:19

ActivityUnitTestCase's startActivity() method needs to be called on the main thread only.

This can be done in the following ways:

  1. Use the @UiThreadTest annotation before your test method:

    @UiThreadTest
    public void testWebViewHasNotSetBuiltInZoomControls() throws Exception {
        Intent intent = new Intent(getInstrumentation().getTargetContext(),
                ContactActivity.class);
        startActivity(intent, null, null);
    }
    
  2. Use the runOnMainSync method of the Instrumentation class:

    public void testWebViewHasNotSetBuiltInZoomControls() throws Exception {
        final Intent intent = new Intent(getInstrumentation().getTargetContext(),
                ContactActivity.class);
    
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                startActivity(intent, null, null);
               }
            });
     }
    

Why am I right?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!