0

I have been trying to launch a simple Activity from a ActivityUnitTestCase, without any success. I guess I'm missing something obvious, but I can't figure out what it is... I managed to run Activities from ActivityInstrumentationTestCase2, but not from ActivityUnitTestCase. The Activity I try to start contains a single Button which launches another Activity when clicked.

First, here is my code for 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());
    }
}

When I run this Test, I get these errors:

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) at com.test.temp.TestingActivityTest.setUp(TestingActivityTest.java:27) at 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 android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90) at junit.framework.TestResult.run(TestResult.java:118) at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49) at junit.framework.TestCase.run(TestCase.java:124) at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63) at 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) at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63) at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90) at 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 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)

The test fails when it tries to launch the Activity. I used a code very similar to this documentation page.

Thank you

Gordak
  • 2,060
  • 22
  • 32
  • 1
    So why are you not using `ActivityInstrumentationTestCase2`? You can use an `ActivityMonitor` to check if your button started the other activity as expected. A guess for your issue is that you may need to run your `button.performClick();` from the UI thread, i.e. with `runOnUiThread(action);`. – Torsten Römer Sep 16 '15 at 10:55
  • Hi. In fact the line button.preformClick() is not reached. The line that throws the Exception is : startActivity(startIntent, null, null); – Gordak Sep 16 '15 at 10:59
  • 1
    I added @UiThreadTest And it works fine now. Apparently startActivity must be ran on the main thread too. – Gordak Sep 16 '15 at 11:03
  • Good idea, nice it works now. – Torsten Römer Sep 16 '15 at 11:05

0 Answers0