简体   繁体   中英

Basic Android JUnit testing problem

I m trying to write some basic junit test but I get the following:

java.lang.RuntimeException: Unable to resolve activity for: Intent { action=android.intent.action.MAIN flags=0x10000000 comp={michaels.pack.POI/michaels.pack.POI} }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:447)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:106)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:84)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:87)
at michaels.pack.test.POITest.setUp(POITest.java:21)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:418)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1520)

Now the class that should be tested is called POI and its in the michaels.pack package!

My test class is:

package michaels.pack.test;

import michaels.pack.POI;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

public class POITest extends ActivityInstrumentationTestCase2<POI> {

    private TextView mView;
    private POI mActivity;
    private String resourceString;

    public POITest() 
    {
        super("michaels.pack.POI", POI.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = (POI)this.getActivity();
        mView = (TextView) mActivity.findViewById(michaels.pack.R.id.username);  
        resourceString= mActivity.getString(michaels.pack.R.id.username);
    }

     public void testPreconditions() 
     {
          assertNotNull(mView);
     }

     public void testText() {
          assertEquals(resourceString,(String)mView.getText());
        }
}

Can anyone see whats wrong with that?

Does it have anything to do with the fact that the project is called POIapp and my "main" class (POI)?

Just to point out that they are in fact two different projects!

Here are the manifests:

Tests manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="michaels.pack.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />
    <instrumentation android:targetPackage="michaels.pack" android:name="android.test.InstrumentationTestRunner" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">

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

Apps manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="michaels.pack" android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:debuggable="true">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".POI" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Register" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".MainMenu" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".ManagePOIsList" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".MapViewClass" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".NewPOIForm" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".ShowPOIsDetails" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".CreatePoiCoordinates" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <activity android:name=".PreferenceClass" android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden|orientation"/>
        <service android:enabled="true" android:name=".MyServiceClass" />
    </application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

Thanks in advance!

Mike

My guess is that you don't have 'use mock location' set up on your emulator, but you have it set up on your device. That's why this error tends to pop up when you have the permission set in the manifest.

I guess the problem is in your manifest files, that's why in the log appears comp={michaels.pack.POI/michaels.pack.POI} and I think it should be comp={michaels.pack/michaels.pack.POI} .

You also need two different projects, one for your application and one for your tests, something that you haven't mentioned either.

Use this constructor:

public POITest() 
{
    super(POI.class);
}

the other is deprecated since API level 8.

It seems to work on an actual device! For some reason it was failing in the emulator!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM