0

I am new to android junit tests, so please bear with me...


FYI, I'm using IntelliJ IDEA.. And I'm running Mac OSX 10.9 (Mavericks) MacBook Pro Mid 2009


So, I run the test that I want to run, it builds and then after it says that it runs the tests, but before the emulator even does anything, it throws a Runtime Exception:

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.NullPointerException
at android.test.suitebuilder.TestMethod.<init>(TestMethod.java:47)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:189)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4335)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

And it's Test Result simply reads "Failed to create tests" and "testSuiteConstructionFailed".


Here's the testing class and method:

package com.antechdevelopment.mymedapp.screens.screens;

import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;

public class ActivityToTestTest extends ActivityInstrumentationTestCase2<ActivityToTest> { 

    public ActivityToTestTest(Class<ActivityToTest> activityClass) {
        super(activityClass);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        setActivityInitialTouchMode(false);

        /*Other prerequisite code to be executed*/
    }

    @UiThreadTest  //This allows me to interact with the views on the app's main thread...
    public void testFoo() {
        /*Code to test with...*/
    }
}

And my test module's AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.antechdevelopment.mymedapp.tests"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="19" />

    <application>
        <uses-library android:name="android.test.runner"/>
    </application>

    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.antechdevelopment.mymedapp"
                     android:label="Tests for com.antechdevelopment.mymedapp"/>
</manifest>

Any help would be GREATLY aprecieated!!!

Thanks, Joshua K

1 Answers1

0

Here is an approach I figured out that uses Android Studio. I am guessing the same will work for IntelliJ IDEA as well. Please note that, I am new to Android development and Java.

The approach uses both Android studio and adb tools.

  1. In Android Studio, for tests based on InstrumentationTestCase, test code can be in the same project as the app code. But that doesn't seem to work for UI test code. A separate module/project needs to be created which compiles into a java library (.jar file). Move the above code to that java library project.
  2. Make JUnit.jar, android.jar and uiautomator.jar as external dependencies. Android.jar should be picked up from sdk/platforms/appropriate-sdk-version
  3. Change JDK dependency of the project to JDK 1.6 (or Java 6). Android claims that it now supports Java 7, but some tools like dx don't work on the jar file generated. Using Java 7 gives the error explained here.
  4. Download Java 6 SE and build with it using these instructions.
  5. The generated jar file needs to be converted to a dex jar file to run on android. The command is 'dx --dex --output=dexed-jar android-studio-generated-jar'
  6. Push the dexed jar file to the android device using 'adb push dexed-jar-path /data/local/tmp'
  7. Execute the UI tests in the jar file using 'adb shell uiautomator runtest dexed-jar -c fully qualified test-or-class name'

The last two steps are the same as the last steps in the instructions from the android developer guide.

Community
  • 1
  • 1