Spe*_*lEd 8 android unit-testing robolectric
我有一个应用程序,如果使用具有自定义方案的特定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不能模仿这一点是否公平,所以不能用来测试这个功能?我是否可以假设我可以/应该单位测试天气我的清单设置正确吗?
谢谢.
我不会以这种方式测试它。您基本上是在测试 Android 的一部分。
我会进行一项测试,即您的AndroidManifest.xml声明是否正确。我会进行一两个测试来检查您的活动是否正确处理数据意图