Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

A step-by-step tutorial for continuous integration with Jenkins for a Red Hat Mobile Native Android application: Part 1

August 9, 2016
Juana Nakfour
Related topics:
Java
Related products:
Red Hat build of Node.js

Share:

    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.

    This post was originally published on the Red Hat Services blog.

    PART 1

    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. Red Hat Mobile Application Platform Instance
    2. Android Studio set up on a development machine.

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

    1. Creating an Android Application using RHMAP
    2. Creating an Android Unit test and Instrumented test
    3. Adding an Android Unit Test
    4. Adding an 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. 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 New→ Directory and call it test. Right click on test directory and choose New → Folder → Java Folder. Check “Change Folder Location” and specify src/test/java. Now the folder is created.

    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 {
    
       @Test
       public void testaddTwoNumbers() {
        assertEquals("addTwoNumbers failed to add two numbers", 300, 
        numberOperationUtility.addTwoNumbers(100,200));
       }
       @Test
       public void testmultiplyTwoNumbers() {
       assertEquals("multiplyTwoNumbers failed to multiply two numbers", 20000, 
       numberOperationUtility.multiplyTwoNumbers(100,200));
       }
    }

    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 inapp/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 toapp/build/reports/androidTests/connected/org.feedhenry.blank.ApplicationTest.html

    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. AddtestInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner” to defaultConfig and add the dependencies

    ///// INSTRUMENT TESTS
    
    // Testing-only dependencies
    androidTestCompile 'junit:junit:4.12'
    
    // Espresso UI Testing
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.2"
    
    // Optional if you need to detect intents.
    androidTestCompile "com.android.support.test.espresso:espresso-intents:2.2.2"
    

    Your build.gradle should look something like this:

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

    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 android.support.test.rule.ActivityTestRule;
    import android.support.test.runner.AndroidJUnit4;
    import android.support.test.filters.LargeTest;
    import org.junit.Rule;
    import org.junit.Test;
    import static android.support.test.espresso.Espresso.onView;
    import static android.support.test.espresso.assertion.ViewAssertions.matches;
    import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
    import static android.support.test.espresso.matcher.ViewMatchers.withText;
    
    @RunWith(AndroidJUnit4.class)
    @LargeTest
    public class MainActivityTest {
       @Rule
      public ActivityTestRule<MainActivity> mActivityRule = new 
    ActivityTestRule<>(MainActivity.class);  
       @Test
       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 refreshapp/build/reports/androidTests/connected/index.html  and you should see total of 3 tests success as seen below:

    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

     

     

    Last updated: October 31, 2023

    Recent Posts

    • Profiling vLLM Inference Server with GPU acceleration on RHEL

    • Network performance in distributed training: Maximizing GPU utilization on OpenShift

    • Clang bytecode interpreter update

    • How Red Hat has redefined continuous performance testing

    • Simplify OpenShift installation in air-gapped environments

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue