Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Android Bootcamp Week 2

BDD with Android
by

Mary-Anne Cosgrove

on 31 May 2016

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Android Bootcamp Week 2

Android Bootcamp Week 2
design by Dóri Sirály for Prezi
The Test Pyramid
Exercise 2: Add Espresso to the Test
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
BDD with Android
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.
Next time...
Next time on
Android Bootcamp:

Layout and Design
Retrospective...
What did we do well?
What could we do better?
What puzzles you?
Why Behaviour Driven?
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.
Exercise 3: Some new behaviour
Exercise 4: Add Unit Tests
FAST
SLOW
JUnit
Robolectric
Espresso
Model
layer
Controller
layer
View layer - Instrument Tests
The Behaviour Driven
Development Cycle
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
Exercise 1: Create a failing functional test

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();
https://github.com/ThoughtWorksAustralia/AndroidBootcampProject#week-2-bdd-with-android

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.
1. Write a second functional test to express this new behaviour:
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:'
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
Exercise 5: Make the Tests 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.
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!
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.
JUnit in Android
Example JUnit Tests
Robolectric
What if Robolectric bites the dust?
Using FEST for Readability
Do Androids Dream of Electric Sheep?
Philip K Dick's 1968 sci-fi classic that inspired Blade Runner
Readability is important:
Matching the Given/When/Then Style
JUnit 3
JUnit 4
Add Hamcrest for
Readable Matchers
Dalvik
Stubs
Shadow Classes
Robolectric test runner
Activity lifecycle via
buildActivity
Create mocks for collaborating classes.
Shadow classes are not perfect
Don't create test subclasses of your real classes
Stub methods with Mockito instead
FEST Android augments Hamcrest matchers with a fluent interface tailored for Android
Behavioural Tests with Espresso
Instrument tests
Run on an android device
Espresso for:
Synchronisation
Readability
ViewMatchers

ViewActions
ViewAssertions
All are customisable
Lint: Another eye on quality
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
Mary-Anne Cosgrove
Senior Developer Consultant
mcosgro@thoughtworks.com
Focus
Structure
Freedom
Communication
Quality
JUnit 3 vs JUnit 4
Model => POJOs
Gradle check
Running in IDE?
Yes, but see BDDinAS.md
Full transcript