Search code examples
androidunit-testingjunitandroid-testingjunit3

Android Unit Test Package Path Errors


I've been plugging along with Android Unit Testing and ran into what appears to be a pathing problem.

First I run this command from my within my project directory

android create test-project -m ../ -p tests -n RoamPayTests

This creates a test project and initializes it up. The auto generated ActivityInstrumentaionTestCase2 works fine. I can create test in here and everything works.

NOTE: Notice that the main activity is in the ui package.

package com.roamdata.roampayx;

import com.roamdata.roampayx.ui.SplashActivity;

import android.test.ActivityInstrumentationTestCase2;

/**
 * This is a simple framework for a test of an Application.  See
 * {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on
 * how to write and extend Application tests.
 * <p/>
 * To run this test, you can type:
 * adb shell am instrument -w \
 * -e class com.roamdata.roampayx.ui.SplashActivityTest \
 * com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner
 */
public class SplashActivityTest extends ActivityInstrumentationTestCase2<SplashActivity> {

    public SplashActivityTest() {
        super("com.roamdata.roampayx", SplashActivity.class);
    }

Here is the auto Generated Android_Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.roamdata.roampayx.tests"
          android:versionCode="1"
          android:versionName="1.0">
    <!-- We add an application tag here just so that we can indicate that
         this package needs to link against the android.test library,
         which is needed when building test cases. -->
    <application>
        <uses-library android:name="android.test.runner" />
    </application>
    <!--
    This declares that this application uses the instrumentation test runner targeting
    the package of com.roamdata.roampayx.  To run the tests use the command:
    "adb shell am instrument -w com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner"
    -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.roamdata.roampayx"
                     android:label="Tests for com.roamdata.roampayx"/>
</manifest>

The problem is when I try to create a new test case.

package com.roamdata.roampayx;
import com.roamdata.roampayx.provider.RoamPay;
import com.roamdata.roampayx.provider.RoamPayProvider;
import android.test.ProviderTestCase2;


public class ProviderTest extends ProviderTestCase2<RoamPayProvider> {
    public ProviderTest() {
        super(RoamPayProvider.class, RoamPay.AUTHORITY);
    }
}

Next I build and install, (no problems here). But then I run

adb shell am instrument -w com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner

And at last I get the following error

android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:
Error in testSuiteConstructionFailed:
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:190)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
    at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
    at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)
    at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4142)
    at android.app.ActivityThread.access$1300(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.roamdata.roampayx.provider.RoamPayProvider
    at ProviderTest.<init>(ProviderTest.java:9)
    ... 18 more

I have the imports to RoamPay and RoamPayProvider. The intellisense is working and I'm getting those references.

Any ideas?


Solution

  • Everything was good except for the constructor. I changed it to

    public RoamPayProviderTest() {
        super(RoamPayProvider.class, "com.roamdata.roampayx");
    }
    

    and now everything works. I guess you can use this question and answers as a nice tutorial on how to setup your unit tests.