如何测试Android类时使用的意图是什么? [英] how to use intent when testing android classes?

查看:125
本文介绍了如何测试Android类时使用的意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些示例code,它似乎并没有为我工作。

here is some sample code, it seems that it doesn't work for me.

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 )

我的清单:

<?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>

我遇到上述即使我设置了构造错误 - >超类的字符串(包)一样,我这个测试项目的manifest.xml中指明的人。
请帮助我。

I encountered the error above even if I set the constructor -> super's string(a package ) same as the one that I've indicated within this test project's manifest.xml. please help me.

推荐答案

我认为你需要做的是使用

I think all you need to do is use

Intent i = new Intent();

请参阅,例如,上<一的答案href=\"http://stackoverflow.com/questions/4320099/how-do-i-write-an-android-junit-test-when-my-activity-relies-on-extras-passed-thr\">this螺纹。

这篇关于如何测试Android类时使用的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆