使用 Robolectric 来测试带有 intent extras 的服务的启动?
Use Robolectric to test starting of a service with intent extras?
我可以使用 Robolectric 来测试 Activity 启动一个带有特定 Bundle 的服务吗? 回答:是的!
我想编写一个基于 Robolectric 的测试来测试我的 MainActivity
以在 intent extras 中传递的特定数字开始 MyService
:
在"MainActivity.java"我有方法
public void startMyService() {
Intent i = new Intent(this, MyService.class);
Bundle intentExtras = new Bundle();
// TODO: Put magic number in the bundle
i.putExtras(intentExtras);
startService(i);
}
这是我的测试用例"MainActivityTest.java":
import ...
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testShallPassMagicNumberToMyService() {
MainActivity activityUnderTest = Robolectric.setupActivity(MainActivity.class);
activityUnderTest.startMyService();
Intent receivedIntent = shadowOf(activityUnderTest).getNextStartedService();
assertNotNull("No intents received by test case!", receivedIntent);
Bundle intentExtras = receivedIntent.getExtras();
assertNotNull("No intent extras!", intentExtras);
long receivedMagicNumber = intentExtras.
getLong(MyService.INTENT_ARGUMENT_MAGIC_NUMBER);
assertFalse("Magic number is not included with the intent extras!",
(receivedMagicNumber == 0L)); // Zero is default if no 'long' was put in the extras
}
}
所以,我的问题是:
我可以使用 Robolectric 吗?
我想我明白了,请看下面的答案...
测试用例不工作,因为它报告 "No intent extras!"。使用调试器我注意到 Intent.putExtras() 在 Robolectric 环境中没有效果。 i.mExtras
(Intent.mExtras
) 属性 在我 运行 我设备上的应用程序时正确设置为 Bundle 引用。当我 运行 测试用例时它是 null
。我想这暗示我的问题的答案是"no",那么我应该放弃这个测试用例还是有什么办法可以完成这个测试?
编辑: 更正了示例 startMyActivity()
方法以反映我实际遇到的问题:似乎 Intent.mExtras
属性 确实除非 Bundle
(?) 中有一些内容,否则不会被填充。这与我使用调试器分析的实时 Android 环境不同。
我展示示例代码的方式并不完全准确!我更新了示例以显示我遇到问题的代码。
事实证明,与真实的 Android 环境相比,Robolectric 环境中 Intent 的管理方式有所不同。使用 Robolectric Intent.mExtras
不会被 Intent.putExtras()
填充,除非 Bundle
中确实有一些内容作为附加内容添加到 Intent
。
我可以使用 Robolectric 来测试 Activity 启动一个带有特定 Bundle 的服务吗? 回答:是的!
我想编写一个基于 Robolectric 的测试来测试我的 MainActivity
以在 intent extras 中传递的特定数字开始 MyService
:
在"MainActivity.java"我有方法
public void startMyService() {
Intent i = new Intent(this, MyService.class);
Bundle intentExtras = new Bundle();
// TODO: Put magic number in the bundle
i.putExtras(intentExtras);
startService(i);
}
这是我的测试用例"MainActivityTest.java":
import ...
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testShallPassMagicNumberToMyService() {
MainActivity activityUnderTest = Robolectric.setupActivity(MainActivity.class);
activityUnderTest.startMyService();
Intent receivedIntent = shadowOf(activityUnderTest).getNextStartedService();
assertNotNull("No intents received by test case!", receivedIntent);
Bundle intentExtras = receivedIntent.getExtras();
assertNotNull("No intent extras!", intentExtras);
long receivedMagicNumber = intentExtras.
getLong(MyService.INTENT_ARGUMENT_MAGIC_NUMBER);
assertFalse("Magic number is not included with the intent extras!",
(receivedMagicNumber == 0L)); // Zero is default if no 'long' was put in the extras
}
}
所以,我的问题是: 我可以使用 Robolectric 吗?
我想我明白了,请看下面的答案...
测试用例不工作,因为它报告 "No intent extras!"。使用调试器我注意到 Intent.putExtras() 在 Robolectric 环境中没有效果。 i.mExtras
(Intent.mExtras
) 属性 在我 运行 我设备上的应用程序时正确设置为 Bundle 引用。当我 运行 测试用例时它是 null
。我想这暗示我的问题的答案是"no",那么我应该放弃这个测试用例还是有什么办法可以完成这个测试?
编辑: 更正了示例 startMyActivity()
方法以反映我实际遇到的问题:似乎 Intent.mExtras
属性 确实除非 Bundle
(?) 中有一些内容,否则不会被填充。这与我使用调试器分析的实时 Android 环境不同。
我展示示例代码的方式并不完全准确!我更新了示例以显示我遇到问题的代码。
事实证明,与真实的 Android 环境相比,Robolectric 环境中 Intent 的管理方式有所不同。使用 Robolectric Intent.mExtras
不会被 Intent.putExtras()
填充,除非 Bundle
中确实有一些内容作为附加内容添加到 Intent
。