无法在ActivityUnitTestCase

时间:2015-09-16 10:42:27

标签: android testing activityunittestcase

我一直试图从Activity发起一个简单的ActivityUnitTestCase,但没有任何成功。我想我错过了一些明显的东西,但我无法弄清楚它是什么......我设法从ActivityInstrumentationTestCase2运行活动,但不是从ActivityUnitTestCase运行。我尝试启动的Activity包含一个Button,它在单击时启动另一个Activity。

首先,这是ActivityUnitTestCase的代码:

public class TestingActivityTest extends ActivityUnitTestCase<TestingActivity> {

    private Intent startIntent;

    public TestingActivityTest() {
        super(TestingActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        startIntent = new Intent(getInstrumentation()
                .getTargetContext(), TestingActivity.class);

    }

    public void testLaunchingSubActivity() {
        startActivity(startIntent, null, null);
        View button = getActivity().findViewById(R.id.forward_button);
        button.performClick();
        assertNotNull(getStartedActivityIntent());
        assertTrue(isFinishCalled());
    }
}

当我运行此测试时,我收到了以下错误:

  

junit.framework.AssertionFailedError at   junit.framework.Assert.fail(Assert.java:48)at   junit.framework.Assert.assertTrue(Assert.java:20)at   junit.framework.Assert.assertNotNull(Assert.java:218)at   junit.framework.Assert.assertNotNull(Assert.java:211)at   android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:147)   在   com.test.temp.TestingActivityTest.setUp(TestingActivityTest.java:27)   在junit.framework.TestCase.runBare(TestCase.java:132)at   junit.framework.TestResult $ 1.protect(TestResult.java:115)at   junit.framework.TestResult.runProtected(TestResult.java:133)at at   android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)   在junit.framework.TestResult.run(TestResult.java:118)at   android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)   在junit.framework.TestCase.run(TestCase.java:124)at   android.support.test.internal.runner.junit3.NonLeakyTestSuite $ NonLeakyTest.run(NonLeakyTestSuite.java:63)   在junit.framework.TestSuite.runTest(TestSuite.java:243)at   junit.framework.TestSuite.run(TestSuite.java:238)at   android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)   在   android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)   在   android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)   在org.junit.runners.Suite.runChild(Suite.java:128)at   org.junit.runners.Suite.runChild(Suite.java:27)at   org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:137)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:115)at at   android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)   在   android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1741)

尝试启动活动时测试失败。我使用的代码与此documentation page非常相似。

谢谢

0 个答案:

没有答案