Android - ActivityUnitTestCase 测试类中 startActivity 方法上的 AssertionFailedError [英] Android - AssertionFailedError on startActivity method in ActivityUnitTestCase test class

查看:39
本文介绍了Android - ActivityUnitTestCase 测试类中 startActivity 方法上的 AssertionFailedError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试模块中的活动.我只是想在测试方法中开始这个活动,但我总是有一个 AssertionFailedError.我在网上搜索了这个问题,但找不到任何解决方案.任何帮助表示赞赏.

I am trying to test an activity in a module. I am just trying to start this activity in the test method, but I always have a AssertionFailedError. I searched the web for this issue but could not find any solution. Any help is appreciated.

这是我的测试课:

public class ContactActivityTest extends ActivityUnitTestCase<ContactActivity> {

    public ContactActivityTest() {
        super(ContactActivity.class);
    }


    @Override
    public void setUp() throws Exception {
        super.setUp();
    }


    public void testWebViewHasNotSetBuiltInZoomControls() throws Exception {
        Intent intent = new Intent(getInstrumentation().getTargetContext(),
                ContactActivity.class);
        startActivity(intent, null, null);
    }


    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

这是错误:

junit.framework.AssertionFailedError
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:147)
at com.modilisim.android.contact.ContactActivityTest.testWebViewHasNotSetBuiltInZoomControls(ContactActivityTest.java:29)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1763)

问候.

推荐答案

ActivityUnitTestCase 的 startActivity() 方法只需要在主线程上调用.

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

这可以通过以下方式完成:

This can be done in the following ways:

  1. 在测试方法之前使用 @UiThreadTest 注释:

@UiThreadTest
public void testWebViewHasNotSetBuiltInZoomControls() throws Exception {
    Intent intent = new Intent(getInstrumentation().getTargetContext(),
            ContactActivity.class);
    startActivity(intent, null, null);
}

  • 使用runOnMainSync Instrumentation 类的方法:

  • 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);
               }
            });
     }
    

  • 为什么我是对的?

    这篇关于Android - ActivityUnitTestCase 测试类中 startActivity 方法上的 AssertionFailedError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆