Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.