In Android, unit testing is based on JUnit and plain use of JUnit is enough to test features that are exclusively based on Java code. In order to test Android specific functionality, however, you need a bunch of wrapper classes just built on top of JUnit. IntelliJ IDEA streamlines most of the tasks around the build of an Android test project: all you do is interacting with the IDE through convenient dialog boxes and wizards.
1. Make sure your code is testable
Unit testing requires that the source code is architected in such a way dependencies between modules are easy to neutralize with mocks. In addition, unit testing requires that functions are well isolated from one another. As is, the code of the MyActivity class is not easy to test. Let's first apply a quick refactoring and then proceed with unit tests.
You open the MyActivity class and select the portion of the code in method TapDroidthat refers to the production of the display message.
Rewrite the TapDroid method in such a way it calls into a newly created public helper method: the getStringForDisplaymethod. Here's the code to enter:
The getStringForDisplay method is now much easier to test and the body of the TapDroid method has been greatly simplified.
2. Create a test project
A test project is a separate module with its own package, manifest and source code. You start by creating a new test module and set HelloDroid as the tested module. This ensures that the test project holds a reference onto the module that contains the source code to test.
A new node is appended to the project named Tests. The project has its own manifest file and src directory. The manifest file in particular links against the android.test library in order to build test cases.
The newly created module has a test file named MyActivityTest right in the src folder. You can add more test files by simply adding more Java classes as shown below:
The test class inherits from extends ActivityInstrumentationTestCase2<T> where T is the name of the activity you're willing to test. Note that the adding the constructor is required as there's no default constructor defined on the parent class.
3. Add a test method
You right click in the editor of the MyActivityTest class and click on Generate. In alternative, you hit Alt + Insert. You get a context menu from where you select Test Method.
IntelliJ IDEA creates a new method stub named testName and it makes it quick and easy for you to change the Name suffix into something more meaningful in the context.
The test prefix in the method name is required if you're using JUnit3, the default testing framework in Android. With JUnit4 you should be using method name's annotations to indicate that a given method has to be processed as a test method.
4. Write the logic for a test method
Internally, the test method first gets a reference to the activity the method is trying to test, calls the target method and finally compares effective results with expected results.
Assertions are implemented through the services of the JUnit framework which needs be properly referenced in the source file.
5. Prepare a build configuration for tests
In order to run tests, you need to have a build configuration that just runs tests. A default configuration is created for you when you set up a test module. To edit settings, you click on Run → Edit configurations, select the one you're interested in and proceed with edits.
In the configuration, you can choose to run all the tests in the module or limit the test to the methods in a given class. You should now be able to run your tests by simply selecting the build configuration and running it.
The test results are green buttons if all went well. Otherwise, you have red buttons to click to know more about failed tests.
By clicking on the green button you can also display and export (say, to HTML) a report of the tests.