Android?????????Robolectric3.0????(?)
?????geniusmart ???????[ 2017/7/5 11:12:48 ] ??????????????? ???????
???????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????????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?????????????????????????????????????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
iOS???????mocha??chai??sinon??istanbul???????????????????????????????????????д?????Java????????7?????????Kiwi?????????????????????????????????Python?????????????????????????????????????Controller????????д?????????10???????????????????Angular????????????Component??Directive??Pipe ???ServiceAndroid????????????????????????????????--Mockito??????iOS UnitTest???????Vue?????????????????Vue ????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????