四、BroadcastReceiver的测试
  首先看下广播接收者的代码
  public class MyReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
  SharedPreferences.Editor editor = context.getSharedPreferences(
  "account", Context.MODE_PRIVATE).edit();
  String name = intent.getStringExtra("EXTRA_USERNAME");
  editor.putString("USERNAME", name);
  editor.apply();
  }
  }
  广播的测试点可以包含两个方面,一是应用程序是否注册了该广播,二是广播接受者的处理逻辑是否正确,关于逻辑是否正确,可以直接人为的触发onReceive()方法,验证执行后所影响到的数据。
  @Test
  public void testBoradcast(){
  ShadowApplication shadowApplication = ShadowApplication.getInstance();
  String action = "com.geniusmart.loveut.login";
  Intent intent = new Intent(action);
  intent.putExtra("EXTRA_USERNAME", "geniusmart");
  //测试是否注册广播接收者
  assertTrue(shadowApplication.hasReceiverForIntent(intent));
  //以下测试广播接受者的处理逻辑是否正确
  MyReceiver myReceiver = new MyReceiver();
  myReceiver.onReceive(RuntimeEnvironment.application,intent);
  SharedPreferences preferences = shadowApplication.getSharedPreferences("account", Context.MODE_PRIVATE);
  assertEquals( "geniusmart",preferences.getString("USERNAME", ""));
  }
  五、Service的测试
  Service的测试类似于BroadcastReceiver,以IntentService为例,可以直接触发onHandleIntent()方法,用来验证Service启动后的逻辑是否正确。
  public class SampleIntentService extends IntentService {
  public SampleIntentService() {
  super("SampleIntentService");
  }
  @Override
  protected void onHandleIntent(Intent intent) {
  SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(
  "example", Context.MODE_PRIVATE).edit();
  editor.putString("SAMPLE_DATA", "sample data");
  editor.apply();
  }
  }
  以上代码的单元测试用例:
  @Test
  public void addsDataToSharedPreference() {
  Application application = RuntimeEnvironment.application;
  RoboSharedPreferences preferences = (RoboSharedPreferences) application
  .getSharedPreferences("example", Context.MODE_PRIVATE);
  SampleIntentService registrationService = new SampleIntentService();
  registrationService.onHandleIntent(new Intent());
  assertEquals(preferences.getString("SAMPLE_DATA", ""), "sample data");
  }
  六、Shadow的使用
  Shadow是Robolectric的立足之本,如其名,作为影子,一定是变幻莫测,时有时无,且依存于本尊。因此,框架针对Android SDK中的对象,提供了很多影子对象(如Activity和ShadowActivity、TextView和ShadowTextView等),这些影子对象,丰富了本尊的行为,能更方便的对Android相关的对象进行测试。
  1.使用框架提供的Shadow对象
  @Test
  public void testDefaultShadow(){
  MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
  //通过Shadows.shadowOf()可以获取很多Android对象的Shadow对象
  ShadowActivity shadowActivity = Shadows.shadowOf(mainActivity);
  ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
  Bitmap bitmap = BitmapFactory.decodeFile("Path");
  ShadowBitmap shadowBitmap = Shadows.shadowOf(bitmap);
  //Shadow对象提供方便我们用于模拟业务场景进行测试的api
  assertNull(shadowActivity.getNextStartedActivity());
  assertNull(shadowApplication.getNextStartedActivity());
  assertNotNull(shadowBitmap);
  }
  2.如何自定义Shadow对象
  首先,创建原始对象Person
  public class Person {
  private String name;
  public Person(String name) {
  this.name = name;
  }
  public String getName() {
  return name;
  }
  }
  其次,创建Person的Shadow对象
  @Implements(Person.class)
  public class ShadowPerson {
  @Implementation
  public String getName() {
  return "geniusmart";
  }
  }
  接下来,需自定义TestRunner,添加Person对象为要进行Shadow的对象(注:Robolectric 3.1 起可以省略此步骤)。
  public class CustomShadowTestRunner extends RobolectricGradleTestRunner {
  public CustomShadowTestRunner(Class<?> klass) throws InitializationError {
  super(klass);
  }
  @Override
  public InstrumentationConfiguration createClassLoaderConfig() {
  InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
  /**
  * 添加要进行Shadow的对象
  */
  builder.addInstrumentedPackage(Person.class.getPackage().getName());
  builder.addInstrumentedClass(Person.class.getName());
  return builder.build();
  }
  }
  后,在测试用例中,ShadowPerson对象将自动代替原始对象,调用Shadow对象的数据和行为
  @RunWith(CustomShadowTestRunner.class)
  @Config(constants = BuildConfig.class,shadows = {ShadowPerson.class})
  public class ShadowTest {
  /**
  * 测试自定义的Shadow
  */
  @Test
  public void testCustomShadow(){
  Person person = new Person("genius");
  //getName()实际上调用的是ShadowPerson的方法
  assertEquals("geniusmart", person.getName());
  //获取Person对象对应的Shadow对象
  ShadowPerson shadowPerson = (ShadowPerson) ShadowExtractor.extract(person);
  assertEquals("geniusmart", shadowPerson.getName());
  }
  }
  七、关于代码
  文章中的所有代码在此:https://github.com/geniusmart/LoveUT
  另外,除了文中所示的代码之外,该工程还包含了Robolectric官方的测试例子,一个简单的登录功能的测试,可以作为入门使用,界面如下图。