Android Unit Testing With Robolectric

23
Unit Testing with Robolectric DroidCon Berlin Danny Preussler, ebay Kleinanzeigen

description

talk from DroidCon 2013 Berlin

Transcript of Android Unit Testing With Robolectric

Page 1: Android Unit Testing With Robolectric

Unit Testing with Robolectric

DroidCon Berlin

Danny Preussler, ebay Kleinanzeigen

Page 2: Android Unit Testing With Robolectric

•  Since 2012 Lead Engineer Android @ ebay Kleinanzeigen

•  Before Team Lead Mobile Enterprise Solutions @ 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

who's that guy?

Page 3: Android Unit Testing With Robolectric

eBay Kleinanzeigen Android app: • free app • 2 Mio downloads • 176 K Daily UV • 4.1/5 stars

(>6K ratings)

Page 4: Android Unit Testing With Robolectric

SOLID FOUNDATION

Flughafen Berlin-Brandenburg, Blick in die Abflughalle by Muns CC BY 3.0 http://commons.wikimedia.org/wiki/File:120513-BER-innen.JPG

Page 5: Android Unit Testing With Robolectric

Raus aus der Garage!

Tidy garage by Bryn Pinzgauer CC 2.0 http://www."ickr.com/photos/12394349@N06/4492572621/in/photostream/

Page 6: Android Unit Testing With Robolectric

• Junit 4 (out for 6+ years!!) • Use all your utilities (i.e. mocking) • Use your coverage framework • Use CI tools without Android plugins (sonar)

Scyscrapers by elminium, CC 2.0 http://www."ickr.com/photos/lumen850/5461476268/in/photostream/

Page 7: Android Unit Testing With Robolectric

Pure JUnit?

java.lang.RuntimeException: Stub!

Android Firewall by Uncalno, CC BY 2.0 "ickr.com/photos/uncalno/8538679708

Page 8: Android Unit Testing With Robolectric

Separate Java and Android Code?

• Android is not Java! • Intent, Bundle, SparseArray, Uri.. not fancy device classes! • Context everywhere • Recreate the System! (interface UI {...} ?)

Great Wall of China (IV) by isawnyu. CC 2.0 http://www."ickr.com/photos/isawnyu/7183821643/in/photostream/

Page 9: Android Unit Testing With Robolectric

Allows loading of Android Classes in pure Java projects

Page 10: Android Unit Testing With Robolectric

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; }

@Test public void createIntentForQuery() { Context c = new AdListActivity(); Intent intent = MyActivity.createIntent(c, "key", "value"); assertNotNull(intent); }

Page 11: Android Unit Testing With Robolectric

In practice (with programs of several thousand lines of code), we haven't discovered any signi!cant di"erences between the two VMs, and we would consider any di"erence to be a bug in one or the other VM.“  https://sites.google.com/site/androiddevtesting/

Is this valid?

Page 12: Android Unit Testing With Robolectric

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; }

@Test public void createIntentForQuery() { Context c = mock(Context.class); Intent intent = MyActivity.createIntent(c, "key", "value"); assertNotNull(intent); }

Page 13: Android Unit Testing With Robolectric

what's this mock thing?

Martin Fowler: „test-doubles ... pre-programmed with

expectations “

Page 14: Android Unit Testing With Robolectric

mocks...

TelephonyManager mgr = mock(TelephonyManager.class); when(mgr.getDeviceId()).thenReturn("123456"); ...

verify(mgr).getDevice();and/or verifyNoMoreInteractions(mgr);

Page 15: Android Unit Testing With Robolectric

MyPreferences pref = spy(new MyPreferences(ctx)); ...

spys...

verify(pref).saveTitle(same(title)); verify(pref).savePersonalData(anyString());

Page 16: Android Unit Testing With Robolectric

Let’s do some Magic…

magic by digitlizedchaos, CC 2.0 "ickr.com/photos/digitizedchaos/4889987210/

in/photostream/

Page 17: Android Unit Testing With Robolectric

@Test public void checkPhoneNumberIsHidden() {

MyActivity activity = new MyActivity(); activity.onCreate(); TextView tv = (TextView) activity.findViewById(R.id.callText); assertEquals(View.GONE, tv.getVisibility());

}

Page 18: Android Unit Testing With Robolectric

long shadows by Matthew Photography CC 2.0 "ickr.com/photos/21560098@N06/8064387720/in/photostream/

let's talk about shadows...

Page 19: Android Unit Testing With Robolectric

Dead End - mid by bennylin0724, CC BY 2.0 flickr.com/photos/benny_lin/191393604

Page 20: Android Unit Testing With Robolectric

2.0

Page 21: Android Unit Testing With Robolectric

2.0

@Test @Values(qualifiers = "fr-land") public void shouldShowBucolicFrenchLandscape() throws Exception { // ... }

•  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:

Page 22: Android Unit Testing With Robolectric

Thank you for coming!

[email protected]

Page 23: Android Unit Testing With Robolectric

Wanna work with us?

eBay Kleinanzeigen

http://goo.gl/3GCJk

mobile.de

http://goo.gl/JYsXM