IntelliJ IDEA 2016.2 Help

Creating Unit Tests

In Android, unit testing is based on JUnit, and plane use of JUnit is enough to test the features that are exclusively based on Java code.

However, to test Android-specific functionality you need a bunch of wrapper classes built on top of JUnit. IntelliJ IDEA streamlines most of the tasks around the build of an Android test project.

1. Make sure your code is testable

Unit testing requires that the source code is composed in such a way that dependencies between modules can be easily neutralized with mocks. In addition, unit testing requires that functions are well isolated from each other.

As is, the code of the HelloDroid class is not easy to test. Let's first apply a quick refactoring before we proceed with unit tests.

  1. Open the HelloDroid class and select the portion of the code in the TapDroid method that refers to the production of the display message:
  2. Rewrite the TapDroid method in such a way so that it calls into a newly created public helper method (GetStringFor Display) as shown below:
    private void TapDroid() { counter++; String temp = getStringForDisplay(counter); message.setText(String.format("You touched the droid %s", temp)); } public String getStringForDisplay(int count) { String temp; switch(count) { case 1: temp = "once"; break; case 2: temp = "twice"; break; default: temp = String.format("%d times", count); } return temp; }

The getStringForDisplay method is now much easier to test, and the body of the TapDroid method has been greatly simplified.

2. Create a test module

Now let's create a new test module and set HelloDroid as the tested module. This ensures that the test module holds a reference onto the module that contains the source code you are going to test.

  1. From the main menu, select File | New | Module to launch the New Module wizard.
  2. On the first page of the wizard, select Android in the left pane, and Test Module on the right:
  3. On the second page, specify the new module name, for example, Tests. Leave the other fields unchanged. The HelloDroid module is specified as the tested module automatically, as at this point, this is the only module in the project.

A new node will be appended to the project named Tests. This module has its own manifest file and and src directory. The manifest file links against the android.test library in order to build test classes.


The newly created module has a test file named HelloDroidTest in the src folder. You can add more test files simply by adding more Java classes as shown below:

public class HelloDroidTest extends ActivityInstrumentationTestCase2<HelloDroid> { public HelloDroidTest() { super("com.example.HelloDroid", HelloDroid.class); } }

The test class inherits from ActivityInstrumentationTestCase2<T> where T is the name of the activity you are going to test.

Note that adding a constructor is required, as there is no default constructor defined for the parent class.

3. Add a test method

In the editor, right-click the HelloDroid test class and click Generate (alternatively, click Alt+Insert). From the popup menu that opens, select Test Method:


IntelliJ IDEA creates a new method stub named testName where you can easily change the Name suffix into something more meaningful in the context:


The test prefix in the method name is required if you are using JUnit 3, the default testing framework in Android. With JUnit 4, you have to use method name annotations to indicate that a given method must 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 it is trying to test, then it calls the target method, and, finally, compares the effective results with the expected results.

Add the following code to the test method:

public void testStringForDisplay() throws Exception { int count = 1; HelloDroid activity = getActivity(); String result = activity.getStringForDisplay(count); Assert.assertEquals(result, "once"); }

Assertions are implemented through the services of the JUnit framework and need to be properly referenced in the source file. Press Alt+Enter when the intention action pops up to reference it:


5. Create a run/debug configuration for tests

In order to run tests, you need to create a dedicated run/debug configuration. A default configuration is created for you automatically when you set up a test module.

To edit its settings, in the main menu select Run | Edit Configurations and select Tests under Android Tests in the left pane:


You can select to run all tests in the module, or limit the test to the methods in a given class.

6. Run a test

To run your tests, make sure the appropriate run/debug configuration is selected in the drop-down list in the top-right corner of the editor, and click the Run button run next to it:


Test results are displayed in the Test Runner tab of the Run tool window that is activated automatically. If a test is completed successfully, a green square icon appears in the top right corner of the editor. If there are warnings, the icon is yellow, and if a test fails - it is red. You can click the icon to get more details.

You can export a test report to a variety of formats by clicking the Export Test Results icon exportToTextFile in the Tests tab toolbar.

See Also

Last modified: 5 September 2016