简体   繁体   English

基本的Android JUnit测试问题

[英]Basic Android JUnit testing problem

I m trying to write some basic junit test but I get the following: 我正在尝试编写一些基本的junit测试,但是得到以下信息:

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! 现在,应该测试的类在michaels.pack包中称为POI!

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)? 该项目称为POIapp和我的“主”类(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} . 我猜问题出在您的清单文件中,这就是为什么日志中显示comp={michaels.pack.POI/michaels.pack.POI} ,我认为应该是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. 另一个自API级别8起已弃用。

It seems to work on an actual device! 它似乎可以在实际设备上运行! For some reason it was failing in the emulator! 由于某种原因,它在模拟器中失败了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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