The dramatic influx of mobile application development has driven many new innovations that make it easier than ever to create compelling, flexible, and secure  applications. This two-part series details my work done at Red Hat’s Open Innovation Labs to capture these mobile innovations in a useful, repeatable way. In part one of this two-part series, I break down the steps to create and unit test a native android application using Red Hat’s Mobile Application Platform. In part two, I show how Jenkins can be used to automate  continuous integration and unit testing of that Mobile app.


Part 1 of this series covers the creation of a native gradle Android application, shows how to add unit and instrumentation tests specific to Android.  

Pre-requisites for this tutorial:

  1. RHMAP Instances
  2. Android Studio setup on a development machine.

The following are the topics covered in today’s post and the video below:

  1. Create Android Application using RHMAP
  2. Create Android Unit test and Instrumented test
  3. Adding Android Unit Test
  4. Adding Android Instrumented Unit Test

Create Android Application using RHMAP

Using an RHMAP instance, create a new Android blank project by selecting Projects→ New Project→ Native Gradle Android Blank Project. Type in a meaningful name and click Create. Make sure to deploy the Cloud App by clicking “Your Project Name”→  Cloud App→ Deploy→ Deploy Cloud App. Now that the CloudApp is running we can download the application and give it a test run.

Create Android Unit test and Instrumented test

Download the blank native Android application just created to your machine by copying the git clone command from the RHMAP application project and pasting it onto your terminal in your machine. Execute git clone and now you should have the source code on your machine. This tutorial assumes you already have Android Studio setup on your development machine. Import the downloaded code to a new Android Studio project and build/run the application. You should get the screen in figure below.  This app displays “Yay, this app is ready to go” after a successful FH.init(). 


After we have verified that this application is working, we can add some example unit tests. There are two major types of unit tests available for Android applications.

  1. Local Unit tests: These tests run on the Java Virtual Machine and have no dependency  on the Android Framework.
  2. Instrumented Unit tests: these tests require some Android framework dependency and need an android device or emulator to run.


Adding Android Unit Test for Gradle

In this tutorial we will write two utility functions that add and multiply two numbers. Add a new java class to your project and add the following code to it:

public class NumberOperationUtility {
   public static int addTwoNumbers(int firstNumber, int secondNumber) {
       return (firstNumber + secondNumber);
   public static int multiplyTwoNumbers(int firstNumber, int secondNumber) {
       return (firstNumber*secondNumber);

Next we will add two unit tests for these two functions. Android Unit tests must be stored in folder <module-name>/src/test/java. The downloaded RHMAP blank Android native app does not have this folder so we will need to create it. Right click on src directory and choose NewDirectory and call it test. Right click on test directory and choose NewFolder→ Java Folder. Check “Change Folder Location” and specify src/test/java. Now the folder is created and you should have something like this.

Screen Shot 2016-06-24 at 9.06.36 AM

Now that the test folder is ready we can start writing our unit tests. Create a new Java Class file in the test folder and add the following code.

import org.feedhenry.blank.numberOperationUtility;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class NumberOperationUtilityTest {
   public void testaddTwoNumbers() {
    assertEquals("addTwoNumbers failed to add two numbers", 300, 
   public void testmultiplyTwoNumbers() {
   assertEquals("multiplyTwoNumbers failed to multiply two numbers", 20000, 

Now we have the two unit tests in we need to tell gradle to build the tests. Since all Unit tests in Android are based on JUnit 4 we must add this dependency in the app/build.gradle file.

dependencies {
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'

Now we are ready to run the Unit tests. We will do this from command line since these are the commands needed by jenkins. In a terminal in your project folder change the permission on gradlew by running chmod +x gradlew

Then run the test using run ./gradlew test

You should see BUILD SUCCESSFUL. To see more details of the results open a browser and open file /app/build/reports/tests/debug/index.html. However for jenkins we will need the .xml format for tests located in /app/build/test-results/debug


Adding Android Instrumented Unit Test to Gradle

Instrumented Unit tests need an Android emulator or a connected Android device. These tests depend on the Android framework, of which some aspects can also be mocked. Instrumented tests are located in app/src/androidTest/java. The RHMAP blank Android application already comes with two tests written in file ApplicationTest.  Connect an Android device to your machine and run ./gradlew connectedAndroidTest. The test results can be viewed from a browser file pointing to app/build/reports/androidTests/connected/org.feedhenry.blank.ApplicationTest.html

We should see something like this indicating two tests ran and are a success.

Screen Shot 2016-06-24 at 12.02.29 PM

Now to make things a little more exciting we will add a simple Instrumented test using Espresso that just checks a string in a view. First we will tell build.gradle to run the tests and get the dependencies. Add testInstrumentationRunner "" to defaultConfig and add the dependencies

// Testing-only dependencies
androidTestCompile 'junit:junit:4.12'
// Espresso UI Testing
androidTestCompile ''
androidTestCompile ""
// Optional if you need to detect intents.
androidTestCompile ""


Your build.gradle should look something like this:

plugins {
   id "com.github.hierynomus.license" version "0.12.0"
apply plugin: ''
android {
   compileSdkVersion 23
   buildToolsVersion "23.0.2"
   defaultConfig {
       applicationId "org.feedhenry.blank"
       minSdkVersion 10
       targetSdkVersion 23
       versionCode 1
       versionName "1.0"
       testInstrumentationRunner ""
   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
   lintOptions {
       abortOnError false
license {
   header rootProject.file('misc/HEADER')
   strictCheck true
dependencies {
   compile 'com.feedhenry:fh-android-sdk:3.0.0'
   compile ''
   testCompile 'junit:junit:4.12'
   testCompile "org.mockito:mockito-core:1.10.19"
   androidTestCompile 'junit:junit:4.12'
   // Espresso UI Testing
   androidTestCompile ''
   androidTestCompile ""
   // Optional if you need to detect intents.
   androidTestCompile ""

Let’s add a test string to our MainActivity view, open res/layout/main_activity.xml and add a textview with a text value “Hello World”. Now let’s add the instrumented test, add a java class file in src/androidTest/java called MainActivityTest and add the code below to it.

package org.feedhenry.blank;
import org.junit.runner.RunWith;
import org.junit.Rule;
import org.junit.Test;
import static;
import static;
import static;
import static;
public class MainActivityTest {
  public ActivityTestRule<MainActivity> mActivityRule = new 
   public void helloWorldTextTest() {
       onView(withText("Hello World")).check(matches(isDisplayed()));

Then use gradlew to run the tests by using ./gradlew connectedAndroidTest. Make sure you have an android device connected or an emulator setup. After the test is done you can refresh app/build/reports/androidTests/connected/index.html  and you should see total of 3 tests success as seen below:

Screen Shot 2016-06-24 at 12.25.38 PM

Now that we have our tests ready we can start our Jenkins setup. Make sure to upload this new code to git so Jenkins has access to it. Run


  • Git add .
  • Git commit -m “Adding Unit and Instrumented Tests”
  • Git push origin master


Connect with Red Hat Consulting, Training, Certification

Learn more about Red Hat Consulting
Learn more about Red Hat Training
Learn more about Red Hat Certification
Subscribe to the Training Newsletter
Follow Red Hat Training on Twitter
Like Red Hat Training on Facebook
Watch Red Hat Training videos on YouTube
Follow Red Hat Certified Professionals on LinkedIn
Creative Commons License