4

I wrote a test project for testing an android application(Application Under Test is my own project). I get the following failure in console as well as in Junit View.

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. Check device log-cat for details

But log-cat does not have a single exception or anything. Log seems same as a successful simple run of application. When i debug the test it fails at teardown() method at following line:

        solo.finishOpenedActivities();

But nothing is reflected on log-cat. Additionally this failure is not uniform for all the test runs. Sometime it fails after first testcase and sometime before first testcase.

public class MainActivityTest extends
    ActivityInstrumentationTestCase2<MainActivity> {

private Solo solo;

public MainActivityTest() throws ClassNotFoundException {
    super(MainActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    solo = new Solo(getInstrumentation(), getActivity());
}

public void testActivityProperlyDisplayed() throws Exception {

    getInstrumentation().waitForIdleSync();
    if (getActivity().getActionBar() != null) {
        assertFalse("ActionBar is shown", getActivity().getActionBar()
                .isShowing());
    } else {
        throw new AssertionFailedError("ActionBar not showing");
    }
}

public void test2(){}
public void test3(){}
// and so on

@Override
protected void tearDown() throws Exception {
    solo.finishOpenedActivities();
    super.tearDown();
}

}

Robotium version is 5.2.1 and is properly imported in project. Also successfully running for other applications. Here's the lines that are printed in console

[2014-07-29 14:19:38 - XMClientTest] Launching instrumentation android.test.InstrumentationTestRunner on UOQ1GYHIQ4
[2014-07-29 14:19:43 - XMClientTest] Sending test information to Eclipse
[2014-07-29 14:19:49 - XMClientTest] Test run failed: Instrumentation run failed due to 'Process crashed.'
[2014-07-29 14:19:49 - XMClientTest] Test run finished
Bhupesh
  • 477
  • 1
  • 8
  • 22

1 Answers1

2

I got the problem. Instrumentation testing can't test an application which has System.exit(0) statement for disposing.

As we know after completion of each test case tearDown() function gets called, And we write solo.finishOpenedActivities() to finish all opened activities(recommended).

When solo.finishOpenedActivities() gets called it calls onDestroy() function of opened activities. I was using System.exit(0) in onDestroy() method which causes to stop Dalvik Virtual Machine(Hence finalization of all objects) that cannot be reinitialized by Instrumentation runner.

Hence try to avoid using System.exit(0); in order to unit test your application.

Bhupesh
  • 477
  • 1
  • 8
  • 22