我有一个应用程序,如果使用具有自定义方案的特定URL,则启动特定活动.例如,如果在webview中使用"myscheme://www.myapp.com/mypath",我的应用程序就会启动.为此,我在清单中配置了intent过滤器,如下所示:
<intent-filter>
<action android:name="android.intent.action.View" />
<data android:scheme="myscheme" android:host="www.myapp.com" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
我想通过编写单元测试来验证这是否有效并继续工作.
@Test
public void testIntentHandling()
{
Activity launcherActivity = new Activity();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myscheme://www.myapp.com/mypath"));
launcherActivity.startActivity(intent);
ShadowActivity shadowActivity = Robolectric.shadowOf(launcherActivity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);
assertNotNull(shadowIntent);
System.out.println(shadowIntent.getAction());
System.out.println(shadowIntent.getData().toString());
System.out.println(shadowIntent.getComponent().toShortString());
assertEquals("com.mycompany", shadowIntent.getComponent().getPackageName());
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我得到的是"shadowIntent.getComponent()"返回null,它应该返回指定我的应用程序和活动的组件.由于大部分工作都是由Android系统完成的,而不是我的应用程序,假设Robolectric不能模仿这一点是否公平,所以不能用来测试这个功能?我是否可以假设我可以/应该单位测试天气我的清单设置正确吗?
谢谢.
我在Eclipse中有一个单独的测试项目,它已经在命令行和Eclipse中成功运行了一段时间.在使用Jenkins运行我的测试时,我遇到了标准InstrumentationTestRunner不以Jenkins支持的xml格式输出的问题.我已经在互联网上阅读使用自定义InstrumentationTestRunner.这在使用ADB的命令行中有效,但在作为Android Test Case运行时在Eclipse中失败.
我已经下载了一个自定义仪器测试运行器(com.neenbedankt.android.test)并将其添加到AndroidManifest中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testedapplication.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<instrumentation
android:name="com.neenbedankt.android.test.InstrumentationTestRunner"
android:targetPackage="com.testedapplication" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
这是我在Eclipse中遇到的错误:
没有为运行测试正确配置[Test Project]:找不到AndroidManifest.xml中检测android.test.InstrumentationTestRunner的targetPackage属性!
你可以看到我在那里设置了targetPackage,所以我不确定我还能做什么?