Unit Tests

The other type of tests are unit tests. They too use JUnit4, which causes some confusion when developers try to determine the difference between the instrumented tests and unit tests.

Where They Run

Unit tests run in Windows, macOS, or Linux, not Android. Unit tests are the same sorts of tests that you would run in an ordinary Java or Kotlin project that had no ties at all to building Android apps.

You can run unit tests on your own development machine. You can also arrange to run unit tests on a CI server or some similar environment.

Because unit tests avoid a lot of the work in getting code over into an Android environment, unit tests can run more quickly. Plus, a development machine or CI server is likely to be faster than Android hardware, and even the Android emulator adds some amount of runtime overhead.

What You Can Test

You can test your non-Android business logic fairly easily, just using standard JUnit tests. However, any code that uses Android-related classes is going to have a problem, though.

You can get past this somewhat by the use of mocking engines, such as Mockito. They allow you to create “fake” Android objects based on real Android classes, where you teach the mocks how to respond to particular method calls.

What the Starter Project Has

Our starter project has a similar set of stuff for unit tests as it did for instrumented tests, with some modifications.

The Annotations and the Test Code

Once again, the class will either be in Java or Kotlin:

package com.commonsware.jetpack.hello

import org.junit.Test

import org.junit.Assert.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
  @Test
  fun addition_isCorrect() {
    assertEquals(4, 2 + 2)
  }
}
package com.commonsware.jetpack.hello;

import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
  @Test
  public void addition_isCorrect() {
    assertEquals(4, 2 + 2);
  }
}

Unit test classes do not normally get a @RunWith annotation. However, we still put @Test annotations on the methods or functions that contain the tests to be run.

In both Java and Kotlin, the tests use JUnit’s assertEquals() to compare two values for equality. This time, the test is to confirm that 2 + 2 equals 4. This test usually succeeds.

How You Run Them

You can run unit tests in much the same way as you run instrumented tests:

What the Test Results Look Like

When you run unit tests, you will not be prompted for a device or emulator to run them on. Unit tests run directly on your development machine, not on an Android device or emulator.

They will run much faster, and they will provide similar output in the “Run” view of Android Studio:

Android Studio, Showing Results of Successful Unit Tests
Android Studio, Showing Results of Successful Unit Tests

The testInstrumentation Dependencies

Any testImplementation statement in your dependencies in your module’s build.gradle file will be compiled into your tests and available for use. The one default testImplementation dependency is junit:junit, which pulls in the JUnit classes.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.