Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

The Promising Start...

The Arduous Journey...

http://bit.ly/OqIOC0

Mary-Anne Cosgrove

Senior Developer Consultant

mcosgro@thoughtworks.com

Behaviour Driven Development in Android Studio

Robolectric is popular in the Android community for unit testing

What if Robolectric bites the dust?

  • Robolectric's shadow classes are not perfect simulators of the Android classes
  • When things go wrong, stubbing methods from your class with Mockito can be a solution
  • Mockito is also great for isolating your unit tests to the class under test, by supplying mocks for collaborating classes.
  • Some recommend creating test subclasses of your real classes. I don't.

Robolectric

  • The Android framework runs in its own virtual machine, called Dalvik
  • When you're developing, the classes available to you are actually stubs
  • This makes it hard to do JUnit testing on classes that use the Android framework, eg Activities
  • Robolectric replaces the Android classes with its own Shadow Classes to allow unit testing
  • Your tests use the Robolectric test runner
  • Robolectric supplies some static methods for driving Activities through the lifecycle
  • You can customise the Shadow Classes if you need to.

Writing unit tests before code improves the structure of our code

Writing feature tests first helps us focus on our user's journey

Refactoring keeps our code clean and maintainable

Fest for Android adds expressive power to unit tests

Using FEST for Readability

they run relatively quickly on the Genymotion emulator

FEST Android augments Hamcrest matchers with a fluent interface tailored for Android

including Behaviour Driven Development

and demonstrating best practices in Agile software development...

...and you can install the Intel HAXM* to improve performance of the Android emulator

including

Android Studio 0.4.3...

*Hardware Accelerated Execution Manager

using the latest tools and frameworks...

I want to teach Java developers how to code Android...

Google has created a new framework for UI testing called Espresso.

Android Studio has built in support for instrument tests

The

Lofty

Goal ...

Some Existing Behaviour...

It's not exactly Cucumber, but...

Behavioural Tests with Espresso

Given I have opened the App

When I enter my name

And click on OK

Then the main activity should open,

And show a welcome message that includes my name.

...And an Espresso Test To Match

  • Behavioural tests, or UI tests, are called Instrument Tests in Android
  • Instrument tests run on a real or emulated android device
  • Espresso eases the pain of Android instrument tests by taking care of synchronisation and providing a fluent, readable interface
  • ViewMatchers find views
  • ViewActions allow your test to interact with views
  • ViewAssertions are what you're testing
  • All three of these are customisable. ViewMatchers and ViewAssertions use Hamcrest Matchers under the covers.

Urgh. We have to DOWNGRADE Gradle from 1.10 to 1.9

We're already using that folder for our instrument tests.

Hmmm.... We'll have to add a separate module for our unit tests

Good Grief! Android bundles in JUnit 3.7 and Robolectric needs 3.8+. We have to add JUnit 4.11 at the head of the classpath

java.lang.NoClassDefFoundError:

ResolvedModuleVersionResult

Gradle in Android Studio is not configured to run unit tests

!!! JUnit version 3.8

or later expected

Class not found: HelloAndroidTest

Android Studio

recognises only ONE folder

for test sources

Heck! Let's remove the support classes and raise the minimum SDK to 14.

huh? can't find parent for StyleData

Tell Google that professional developers need support for unit testing

http://bit.ly/1dBrAvb

Not a great solution. We'll keep looking for a better one.

The

Break- through...

The

Conver- sation...

The

Brick

Wall...

Contribute to the Gradle Android test plugin

http://bit.ly/1cJtdDE

Thanks for coming!

Build interest in testing in your Android community

Visit Android Bootcamp on Github for more on BDD in Android

http://bit.ly/1lHDlov

Call your local MP

OK, we'll use Novoda's gradle-android-test-plugin

It all adds

up to fewer bugs

We have to update the gradle build script or the run configuration to compile the test classes first

Keep an eye on the brick wall at

http://bit.ly/1hUTYJE

Learn more about creating dynamic, engaging presentations with Prezi