???????BroadcastReceiver?????
??????????1???????????
????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));
????//???2???????????????????????
????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?????
????public class CustomShadowTestRunner extends RobolectricGradleTestRunner {
????public CustomShadowTestRunner(Class<?> klass) throws InitializationError {
????super(klass);
????}
????@Override
????public InstrumentationConfiguration createClassLoaderConfig() {
????InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
????/**
????* ????????Shadow?????
????*/
????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?????????????????????????????????????????????????????????

????????ο?????
????http://robolectric.org
????https://github.com/robolectric/robolectric
????http://tech.meituan.com/Android_unit_test.html