Bring Back the Fun to Testing Android Apps with Robolectric

Post on 10-May-2015

9.457 views 2 download

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

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

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

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

Testing Android Apps with Robolectric

What’s the problem?

Why is Robolectric a solution?

Demo

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

What’s the problem?

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

What’s the problem?

Running tests requires:

• Dexing

• Packaging

• Installation on device / emulator

Dexing

Packaging

Installation

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

Dexing Packaging Installation

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

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

There is another problem…

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

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

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:

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

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

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

Contact

Peter Friesepeter.friese@zuhlke.com

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

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

Image credits

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

The End