20

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.

This is my test class:

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

And this is the error:

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)

Regards.

ismail
  • 201
  • 1
  • 4
  • 1
    Have you tried with an empty activity or could it be that `ContactActivity` code is responsible for the error? – Simas Apr 15 '15 at 09:33
  • @Simas it might well be, the problem is which part of the code. I tried with an extension of either `FragmentActivity` and `Activity`, and I emptied the latter's code and it still crashed with the same issue. – Vince Apr 16 '15 at 09:09
  • I believe you need to have the startActivity(...) code in setUp() Can you check this, and if so I'll add an answer for you to accept. – Doug Edey Apr 17 '15 at 02:03
  • @MGranja That's the JUnit 3 that deals with calling the test method – Vince Apr 17 '15 at 17:13
  • @DougEdey According to the [javadoc](https://developer.android.com/reference/android/test/ActivityUnitTestCase.html#startActivity%28android.content.Intent,%20android.os.Bundle,%20java.lang.Object%29) it should NOT be called in setUp() (because of a ClassLoader issue), but I tried anyway to call in eithersetUp or test method, with the same issue. – Vince Apr 17 '15 at 17:16
  • @Xcihnegn that's the line: `startActivity(intent, null, null);` – Vince Apr 17 '15 at 17:17
  • actually I saw one sample test codes that calls `startActivity` from `setup `, so could just put in setup without put in test method – Xcihnegn Apr 17 '15 at 21:20
  • Is the Activity defined in the manifest file? I tracked down the exception, and it ends up here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/app/Instrumentation.java?av=f#1763 (ActivityNotFoundException) – kevinpelgrims Apr 17 '15 at 21:47
  • @Vince are you seeing this exact error? – Jared Burrows Apr 20 '15 at 16:11
  • try to update java veraion from 1.6 or upgrade ane run again. May be it work. If upgrade then try to down grade try both. – parik dhakan Apr 20 '15 at 15:57

1 Answers1

12

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?

appoll
  • 2,910
  • 28
  • 40
  • I'm sorry that I could not verify the answer to give you my initial bounty. But the annotation solved the issue, so here it is repaired, you deserved this bounty. Thanks! – Vince Apr 30 '15 at 09:41
  • This answer solves the issue, it would be great if the OP of question accepts this as answer. – Prudhvi Jul 31 '15 at 18:53
  • You might want to extend `ActivityInstrumentationTestCase2` instead. – jayeffkay Dec 16 '15 at 11:53