Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Unit Testingwith Robolectric	DroidCon Berlin	Danny Preussler, ebay Kleinanzeigen
•  Since 2012 Lead Engineer Android @ebay Kleinanzeigen	•  Before Team Lead Mobile EnterpriseSolutions @ Cortado	•  C++ De...
eBay KleinanzeigenAndroid app:	• free app	• 2 Mio downloads	• 176 K Daily UV	• 4.1/5 stars(>6K ratings)
SOLID 	FOUNDATION	Flughafen Berlin-Brandenburg, Blick in die Abflughalle by Muns CC BY 3.0http://commons.wikimedia.org/wik...
Raus aus der Garage!	Tidy garage by Bryn Pinzgauer CC 2.0http://www."ickr.com/photos/12394349@N06/4492572621/in/photostrea...
• Junit 4(out for 6+ years!!)	• Use all yourutilities(i.e. mocking)	• Use your coverageframework	• Use CI toolswithout And...
Pure JUnit?	java.lang.RuntimeException: Stub! 	Android Firewall by Uncalno, CC BY 2.0 "ickr.com/photos/uncalno/8538679708
Separate Java and Android Code?	• Android is not Java!		• Intent, Bundle,SparseArray, Uri.. notfancy device classes!		• Co...
Allows loading of Android Classesin pure Java projects
static Intent createIntent(Context context, String query,String value) {Intent i = new Intent(context, AdListActivity.clas...
In practice (with programs of several thousand lines ofcode), we havent discovered any signi!cant di"erencesbetween the tw...
static Intent createIntent(Context context, String query,String value) {Intent i = new Intent(context, AdListActivity.clas...
whats this mock thing?Martin Fowler:„test-doubles ... pre-programmed withexpectations “
mocks...	TelephonyManager mgr =mock(TelephonyManager.class);when(mgr.getDeviceId()).thenReturn("123456");...verify(mgr).ge...
MyPreferences pref = spy(new MyPreferences(ctx))...spys...	verify(pref).saveTitle(same(title));verify(pref).savePersonalDa...
Let’s do some Magic…	magic by digitlizedchaos, CC 2.0"ickr.com/photos/digitizedchaos/4889987210/in/photostream/
@Testpublic void checkPhoneNumberIsHidden() {MyActivity activity = new MyActivity();activity.onCreate();TextView tv = (Tex...
long shadows by Matthew Photography CC 2.0 "ickr.com/photos/21560098@N06/8064387720/in/photostream/	lets talk about shadow...
Dead End - mid by bennylin0724, CC BY 2.0flickr.com/photos/benny_lin/191393604
2.0
2.0	@Test @Values(qualifiers = "fr-land")public voidshouldShowBucolicFrenchLandscape() throwsException {// ...}•  less sha...
Thank you 	for coming!	dpreussler@ebay.com
Wanna work with us?	eBay Kleinanzeigenhttp://goo.gl/3GCJk	mobile.dehttp://goo.gl/JYsXM
Upcoming SlideShare
Loading in …5
×

Android Unit Testing With Robolectric

3,270 views

Published on

talk from DroidCon 2013 Berlin

Published in: Technology, Education
  • Be the first to comment

Android Unit Testing With Robolectric

  1. 1. Unit Testingwith Robolectric DroidCon Berlin Danny Preussler, ebay Kleinanzeigen
  2. 2. •  Since 2012 Lead Engineer Android @ebay Kleinanzeigen •  Before Team Lead Mobile EnterpriseSolutions @ Cortado •  C++ Developer @ PSI AG •  Started career as developer @ Alcatel •  articles in: android360, mobile Technology,Java Magazin •  speaker: Blackberry Developer Conference SF,DroidCons Believe in mobile whos that guy?
  3. 3. eBay KleinanzeigenAndroid app: • free app • 2 Mio downloads • 176 K Daily UV • 4.1/5 stars(>6K ratings)
  4. 4. SOLID FOUNDATION Flughafen Berlin-Brandenburg, Blick in die Abflughalle by Muns CC BY 3.0http://commons.wikimedia.org/wiki/File:120513-BER-innen.JPG
  5. 5. Raus aus der Garage! Tidy garage by Bryn Pinzgauer CC 2.0http://www."ickr.com/photos/12394349@N06/4492572621/in/photostream/
  6. 6. • Junit 4(out for 6+ years!!) • Use all yourutilities(i.e. mocking) • Use your coverageframework • Use CI toolswithout Androidplugins (sonar) Scyscrapers by elminium, CC 2.0 http://www."ickr.com/photos/lumen850/5461476268/in/photostream/
  7. 7. Pure JUnit? java.lang.RuntimeException: Stub! Android Firewall by Uncalno, CC BY 2.0 "ickr.com/photos/uncalno/8538679708
  8. 8. Separate Java and Android Code? • Android is not Java! • Intent, Bundle,SparseArray, Uri.. notfancy device classes! • Context everywhere • Recreate theSystem! (interfaceUI {...} ?) Great Wall of China (IV) by isawnyu. CC 2.0 http://www."ickr.com/photos/isawnyu/7183821643/in/photostream/
  9. 9. Allows loading of Android Classesin pure Java projects
  10. 10. static Intent createIntent(Context context, String query,String value) {Intent i = new Intent(context, AdListActivity.class);i.putExtra(AppConstants.QUERY, query);i.putExtra(AppConstants.VALUE, queryString);return i;}@Testpublic void createIntentForQuery() {Context c = new AdListActivity();Intent intent =MyActivity.createIntent(c, "key", "value");assertNotNull(intent);}
  11. 11. In practice (with programs of several thousand lines ofcode), we havent discovered any signi!cant di"erencesbetween the two VMs, and we would consider anydi"erence to be a bug in one or the other VM.“ https://sites.google.com/site/androiddevtesting/ Is this valid?
  12. 12. static Intent createIntent(Context context, String query,String value) {Intent i = new Intent(context, AdListActivity.class);i.putExtra(AppConstants.QUERY, query);i.putExtra(AppConstants.VALUE, queryString);return i;}@Testpublic void createIntentForQuery() {Context c = mock(Context.class);Intent intent =MyActivity.createIntent(c, "key", "value");assertNotNull(intent);}
  13. 13. whats this mock thing?Martin Fowler:„test-doubles ... pre-programmed withexpectations “
  14. 14. mocks... TelephonyManager mgr =mock(TelephonyManager.class);when(mgr.getDeviceId()).thenReturn("123456");...verify(mgr).getDevice();and/orverifyNoMoreInteractions(mgr);
  15. 15. MyPreferences pref = spy(new MyPreferences(ctx))...spys... verify(pref).saveTitle(same(title));verify(pref).savePersonalData(anyString());
  16. 16. Let’s do some Magic… magic by digitlizedchaos, CC 2.0"ickr.com/photos/digitizedchaos/4889987210/in/photostream/
  17. 17. @Testpublic void checkPhoneNumberIsHidden() {MyActivity activity = new MyActivity();activity.onCreate();TextView tv = (TextView)activity.findViewById(R.id.callText);assertEquals(View.GONE, tv.getVisibility());}
  18. 18. long shadows by Matthew Photography CC 2.0 "ickr.com/photos/21560098@N06/8064387720/in/photostream/ lets talk about shadows...
  19. 19. Dead End - mid by bennylin0724, CC BY 2.0flickr.com/photos/benny_lin/191393604
  20. 20. 2.0
  21. 21. 2.0 @Test @Values(qualifiers = "fr-land")public voidshouldShowBucolicFrenchLandscape() throwsException {// ...}•  less shadows, more real code(goal is to remove 99% of shadows) •  since alpha2: support package complete shadow free •  aims for sherlock supported, library support •  performance boost •  Value quali$ers:
  22. 22. Thank you for coming! dpreussler@ebay.com
  23. 23. Wanna work with us? eBay Kleinanzeigenhttp://goo.gl/3GCJk mobile.dehttp://goo.gl/JYsXM

×