Bring Back the Fun to Testing Android Apps with Robolectric

15
Peter Friese - @peterfriese - http://www.peterfriese.de Bring Back the Fun to Testing Android Apps with Robolectric

description

A short introduction why Robolectric will make testing more fun on Android and how to set it up in Eclipse.

Transcript of Bring Back the Fun to Testing Android Apps with Robolectric

Page 1: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Bring Back the Fun to Testing Android Apps

with Robolectric

Page 2: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Bring Back the Fun to Testing Android Apps

with Robolectric

Page 3: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Testing Android Apps with Robolectric

What’s the problem?

Why is Robolectric a solution?

Demo

Page 4: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

What’s the problem?

Page 5: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

What’s the problem?

Running tests requires:

• Dexing

• Packaging

• Installation on device / emulator

Dexing

Packaging

Installation

Page 6: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Dexing Packaging Installation

Page 7: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Robolectric to the rescue!

Run tests in a regular JVM, not on Dalvik VM

• Dexing

• Packaging

• Installation on device / emulator

Page 8: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

There is another problem…

protected void onCreate(Bundle savedInstanceState) { throw new RuntimeException("Stub!"); }

Page 9: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Robolectric Solution:

Shadow Objects

• Intercept class loading

• Rewrite method bodies (using ASM)

• Intercept calls to Android classes, record state in shadow classes

Page 10: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

FEST - Readability for Your Tests

assertEquals(View.GONE, textView.getVisibility()); !java.lang.AssertionError: expected:<8> but was:<0>

Plain JUnit:

assertThat(textView).isGone(); !java.lang.AssertionError: Expected to be gone but was visible

FEST:

Page 11: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Demo

See http://www.peterfriese.de/unit-testing-android-apps-with-robolectric-and-eclipse/ for a step-by-step description of how to set up Robolectric in Eclipse

Page 12: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Electrified? Here’s where to get Robolectric / FEST

Web: http://ww.robolectric.org Github: https://github.com/robolectric/robolectric

Web: http://square.github.io/fest-android/ Github: https://github.com/square/fest-android

Page 13: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Contact

Peter [email protected]

!Zuhlke Engineering Ltd43 Whitfield StLondonW1T 4HD!+44 7825 688 244

Page 14: Bring Back the Fun to Testing Android Apps with Robolectric

Peter Friese - @peterfriese - http://www.peterfriese.de

Image credits

• Snail - http://www.wallsfeed.com/snail-journey-leaf/

Page 15: Bring Back the Fun to Testing Android Apps with Robolectric

The End