android - 测试Android类时如何使用 Intent ?

标签 android testing android-intent

这里是一些示例代码,它似乎不适合我。

public class CropImageTest extends ActivityInstrumentationTestCase2<CropImage> {

    private Instrumentation mInstrumentation;
    private CropImage mActivity;
    private String filename = "/mnt/sdcard/DCIM/Camera/2011-05-12 09.22.56.jpg";
    private int aspectX = 1;
    private int aspectY = 1;
    private boolean scale = true;

    public CropImageTest() {
        super("hk.com.novare.android.cropimage", CropImage.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        this.mInstrumentation = getInstrumentation();
        Intent i = new Intent(mInstrumentation.getContext(), CropImage.class);

        i.putExtra("image-path", filename);
        i.putExtra("aspectY", aspectY);
        i.putExtra("aspectX", aspectX);
        i.putExtra("scale", scale);
        setActivityIntent(i);
        mActivity = this.getActivity();  
    }

    public void testExtras() {
        String str = "";
        str = mActivity.getIntent().getStringExtra("image-path");
        assertEquals(filename, str);
    }
}

遇到错误:

Unable to resolve activity for: Intent ( has Extras )

我的 list :

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

    <uses-sdk android:minSdkVersion="8"/>
    <instrumentation
            android:targetPackage="hk.com.novare.android.cropimage"
            android:name="android.test.InstrumentationTestRunner"/>
    <application
            android:icon="@drawable/icon"
            android:label="@string/app_name">
        <uses-library android:name="android.test.runner"/>
    </application>
</manifest>

即使我将构造函数 -> super 的字符串(一个包)设置为与我在此测试项目的 manifest.xml 中指示的字符串相同,我也遇到了上述错误。 请帮助我。

最佳答案

我认为你需要做的就是使用

Intent i = new Intent();

例如,参见 this thread 上的答案.

关于android - 测试Android类时如何使用 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6464028/

相关文章:

android - 未触发 ActivityNotFoundException 打开下载的 PDF 文件

java - 如何在 ASyncTask 剥离完成后结束 Android 服务?

android - GridView 在按下后退按钮时被清除(当键盘可见时)

android - 具有独立工作可点击 ImageView 的小部件的多个实例

java - RMI 负载测试工具?

android - 在实际设备中安装 .apk 文件仅用于测试

java - 从另一个应用 Intent 打开时 Gmail 应用崩溃

java - Android实现同步音效

Android 与 Google Cloud SQL 的连接

java - 在 Java 中测试 try-catch block