1. Introduction
Electronic supplementary material
The online version of this chapter (doi: 10.1007/978-1-4842-9701-8_1 ) contains supplementary material, which is available to authorized users.
For a while now, Agile development has been problematic for Android developers. There have been a number of ways to test the user interface (UI), such as Robotium or Monkey Runner, but before Android Studio 1.1, unit testing was hard to use, hard to configure, and quite challenging to implement on the Android platform.
Google would argue, no doubt, that in the past you could use JUnit3-style unit testing. But for anyone from classic Java development this was a dramatic backward step in time. Developers would stumble along hacking together a JUnit4 development environment using a number of third-party tools. More likely than not they would simply give up as the ever-increasing series of mutually incompatible library dependencies would finally wear them down. Because there simply wasnt the toolbox for the Android developer, Agile development on the mobile platform was immature and reminiscent of Java development in the early 2000s.
Thankfully all this has changed - Android now supports JUnit4 and Android developers can now return to unit testing. Its early days yet in the world of Android JUnit4 testing world and the documentation is thin on the ground, so in this book were going to show practical ways to get your unit testing up and running using Android Studio. Well also look at how this can be complemented by other UI-specific Android testing libraries such as Espresso to create a complete Agile testing framework for Android developers.
Hello, World Unit Test
Before we go any further lets look at a simple unit test. For demonstration purposes we can use the Add method from the Google Calculator example, which is available from ).
Listing 1-1 . Add Method from Googles Calculator Example
public double add(double firstOperand, double secondOperand) {
return firstOperand + secondOperand;
}
Listing shows a very simple unit test, which tests if the Add method can add two numbers correctly.
Listing 1-2 . Test Method for Add Method from Calculator Example
@Test
public void calculator_CorrectAdd_ReturnsTrue() {
double resultAdd = mCalculator.add(3, 4);
assertEquals(7, resultAdd, 0);
}
Unit tests use assertions to make sure the method provides an expected result. In this case were using assertEquals to see if the Add method returns 7 when adding 3 to 4. If the test works, then we should see a positive or green result, and if it doesnt, then well see a red result in Android Studio.
Understand the Benefits of Using an Agile Approach to Android Development
If youre new to Agile development youre probably wondering how Agile can improve the development process.
At its most basic, Agile, and unit testing in particular, helps you to
Catch more mistakes, earlier in the development process
Confidently make more changes
Build in regression testing
Extend the life of your codebase
If you write unit tests and they cover a significant portion of your code then youre going to catch more bugs. You can make simple changes to tidy up the code or more extensive architectural changes, run your unit tests, and, if they all pass, be confident that you didnt introduce any subtle defects. The more unit tests you write, the more you can regression test your app whenever you change the code without fear. And once you have a lot of unit tests, then it becomes a regression test suite that allows you to have the confidence to do things you wouldnt otherwise attempt.
Unit tests mean you no longer have to program with a leave well enough alone mind-set. You can now make significant changes (changing to a new database, updating your back-end application programming interface (API), changing to a new material design theme, etc.) and be confident that your app is behaving the same as before you made the changes since all the tests execute without any errors.
Explore the Agile Testing Pyramid for Android
There are several types of tests you need in your test suite to make sure your app is fully tested. You should have Unit Tests for the component- or method-level functionality, API or Acceptance Tests for any back-end RESTful APIs, and GUI (graphical user interface) Tests for Android activities and general application workflow.
The classic Agile Test Pyramid first appeared in Succeeding with Agile by Mike Cohn ( Pearson Education, 2010). This is a good guide for the relative quantity of each type of test your app is going to need (see Figure ).
Figure 1-1.
Agile Test Pyramid
Create Hello World Unit Test in Android
In the following example we show how to create our simple unit test example in Android Studio. This should return true assuming adding two numbers in the calculator Android app works correctly.
To set up and run a unit test you need to perform the following tasks:
Prerequisites: Android Plugin for Gradle version 1.1.x
Create the src/test/java folders
Add JUnit:4:12 dependency in build.gradle (app) file
Choose unit tests test artifact in Build Variant
Create unit tests
Right-click tests to run tests
Click File Project Structure and make sure the Android Plugin version is greater than 1.1. In Figure the Android Plugin version is 1.2.3 so were good to go.
Next we need to create the src/test/java folders for our unit test code. For the moment this seems to be hard-coded to this directory. So change to Project view to see the file structure and create the folders (see Figure ). Alternatively, in Windows create the folders using the file explorer or on a Mac use the command line on a terminal window to make the changes. Dont be worried if the folders dont show up when you go back to the Android view in Android Studio. Theyll show up when we change to unit tests in the Build Variant window.
Figure 1-3.
Change to Project view
Add junit library to the dependencies section in the build.gradle (app) file as shown in Figure .
Figure 1-4.
Modify the build.gradle file
Choose the Unit Tests test artifact in Build Variants and use the debug build (see Figure ). The test code directory should now also appear when youre in the Android view of your app.