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

Android Bootcamp Week 2

BDD with Android

Mary-Anne Cosgrove

Senior Developer Consultant

mcosgro@thoughtworks.com

https://github.com/ThoughtWorksAustralia/AndroidBootcampProject#week-2-bdd-with-android

Last week:

  • We created a new project and explored its structure

  • We began work on a game where the player can hunt for 'treasures', starting by allowing the player to enter their name and welcoming them.

This week:

  • We will add a framework for behaviour driven development, including a unit test and a functional test

  • We will allow the user to view pages for the treasure list, the high scores, and the map.

Retrospective...

Next time...

Next time on

Android Bootcamp:

Layout and Design

  • What did we do well?
  • What could we do better?
  • What puzzles you?

Why Behaviour Driven?

Focus

Structure

Quality

Freedom

Communication

Summary

  • Write the most unit tests
  • Write fewer integration tests
  • Write the least functional tests
  • Start with the functional test
  • Then a unit test
  • Then write the code to make the tests pass
  • Try Espresso, Robolectric, Fest and Mockito

SLOW

The Test Pyramid

Exercise 3: Some new behaviour

Exercise 2: Add Espresso to the Test

1. Write a second functional test to express this new behaviour:

Readability is important:

Matching the Given/When/Then Style

Espresso

Some Existing Behaviour...

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

Given I have entered my name,

When I open the menu

Then it should have an option 'Treasure List'

And When I click on that option

Then I should see a heading on the page saying 'Find these Treasures:'

Behavioural Tests with Espresso

View layer - Instrument Tests

  • Instrument tests
  • Run on an android device
  • Espresso for:
  • Synchronisation
  • Readability
  • ViewMatchers
  • ViewActions
  • ViewAssertions
  • All are customisable

1. Download Espresso bundled jar from

https://code.google.com/p/android-test-kit/source/browse/bin/espresso-standalone/espresso-1.1-SNAPSHOT-bundled.jar

Save it in src/androidTest/libs

2. Change build.gradle dependencies:

dependencies {

compile 'com.android.support:appcompat-v7:+'

androidTestCompile fileTree(dir: 'src/androidTest/libs', includes: ['*.jar'])

}

3. Resync Android Studio with

Tools > Android > Sync Project With Gradle Files

4. Now write the test method (JUnit 3 style only is supported)

Try this:

Espresso.onView(ViewMatchers.withId(R.id.player_field)).perform(ViewActions.typeText("Fred"));

Then static import the methods for better readability.

5. Complete the test shown earlier

6. Start up the emulator and check that it runs and passes.

FAST

Robolectric

Exercise 1: Create a failing functional test

2. Make the test compile.

3. Run the test and watch it fail (hopefully for the right reason!)

4. Optional: Write similar tests for 'High Scores' and 'Map' menu options

Controller

layer

Lint: Another eye on quality

Using FEST for Readability

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

What if Robolectric bites the dust?

  • Gradle is set up to run Lint automatically
  • Lint reports some common issues
  • Sometimes your build will fail due to Lint errors
  • View the Lint report at AndroidBootcampProject/AndroidBootcamp/build/lint-results.html
  • Create mocks for collaborating classes.
  • Shadow classes are not perfect
  • Don't create test subclasses of your real classes
  • Stub methods with Mockito instead

Robolectric

JUnit

  • Dalvik
  • Stubs
  • Shadow Classes
  • Robolectric test runner
  • Activity lifecycle via buildActivity

Do Androids Dream of Electric Sheep?

Philip K Dick's 1968 sci-fi classic that inspired Blade Runner

1. Create src/androidTest/java (exact names are important)

2. Right click to create a new package com.thoughtworks.androidbootcamp.test (name MUST be same as base package name plus .test)

3. Right click to create a new Java class in this package. Extend this class from ActivityInstrumentationTestCase2<HelloAndroid>

4. Create a no-arg constructor that calls super with package name and HelloAndroid.class (suppress the deprecated warning)

5. Start the Genymotion emulator

6. Now should be able to run from Android Studio (right click on the test file) or from command line with gradle connectedCheck

7. To ensure the test is running correctly, make it fail by adding

fail();

Add Hamcrest for

Readable Matchers

Model

layer

JUnit 3

Example JUnit Tests

JUnit 4

Exercise 5: Make the Tests Pass!

Exercise 4: Add Unit Tests

JUnit in Android

  • JUnit 3 vs JUnit 4
  • Model => POJOs
  • Gradle check
  • Running in IDE?
  • Yes, but see BDDinAS.md

1. Modify the onCreateVIew event of the PlaceholderFragment so that it calls either showTreasureList, showHighScores or showMap, depending on which section number is passed in.

2. Implement the three methods to set the text of the section label to the heading for the page.

3. Run the tests. If all is well they should pass!

1. Go to Novoda's Gradle Android Test Plugin from

https://github.com/novoda/gradle-android-test-plugin

2. Add a new module to your Android project to contain the unit tests

3. Create a JUnit test to test the Game class (just test the player name for now - this test should pass)

4. Write a Robolectric test to test three new methods of the HelloAndroid controller - showTreasureList, showMap and showHighScores.

5. Run the tests so you can see them fail.

Use gradle check --info on the command line from the AndroidBootcampProject directory. Results are reported at AndroidBootcampProject/AndroidBootcampTest/build/test-report/index.html

6. Optional: Persuade Android Studio to run the tests. This is, unfortunately, not easy. Step by step instructions are at the Android Bootcamp Github.

The Behaviour Driven

Development Cycle

design by Dóri Sirály for Prezi

The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Learn more about creating dynamic, engaging presentations with Prezi