<p>This article is designed to get a basic understanding of how to add JUnit and espresso testing in the android application with the help of a simple <span style="color: #008000;"><b>calculator app</b></span>.</p>
<p>CalculatorApp will perform operations like addition, subtraction, multiplication, division, percentage only on two numbers. In CalculatorApp we will write Unit and UI test cases on all the operations.</p>
<p>When you run the application <strong><span style="color: #0000ff;">CalculatorApp</span></strong> it will give you the output as shown below:</p>
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="z8s6IcMrfVM" title="Android Junit and Espresso Testing using CalculatorApp example"><a placeholder href="https://youtu.be/z8s6IcMrfVM"><img src="https://i.ytimg.com/vi/z8s6IcMrfVM/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android Junit and Espresso Testing using CalculatorApp example"></a></amp-youtube></p>
<p><span style="color: #0000ff;"><strong><span style="color: #008000;">Get Complete GITHUB code from</span> <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://github.com/arunk7839/CalculatorApp">here</a></span>.</strong></span></p>
<h3><strong><span style="color: #000080;">Testing</span></strong></h3>
<p class="p1">By running tests against your app consistently, you can verify your app&#8217;s correctness, functional behavior, and usability before you release it publicly.</p>
<p class="p1"><span class="s1">A typical project in Android Studio contains two directories in which you place tests. </span></p>
<ul class="ul1">
<li class="li2"><span class="s2">The </span><span style="color: #0000ff;"><strong><span class="s3">androidTest</span></strong></span><span class="s2"> directory should contain the tests that run on real or virtual devices. Such tests include integration tests, end-to-end tests, and other tests where the JVM alone cannot validate your app&#8217;s functionality.</span></li>
<li class="li2"><span class="s2">The </span><span style="color: #0000ff;"><strong><span class="s3">test</span></strong></span><span class="s2"> directory should contain the tests that run on your local machine, such as unit tests.</span></li>
</ul>
<h3><strong><span style="color: #000080;">Testing Tools in android</span></strong></h3>
<p class="p1">There are many tools that can be used for testing android applications. Some are official like Junit, Espresso and some are third-party tools that can be used to test android applications. In this article, we will talk about these two tools to test android applications.</p>
<ul class="ul1">
<li class="li2">Junit</li>
<li class="li2">Espresso</li>
</ul>
<h4><strong><span style="color: #0000ff;">JUnit</span></strong></h4>
<p><strong><span style="color: #008000;">Junit</span></strong> is a “Unit Testing” framework for Java Applications.</p>
<p>Unit testing is a way of testing the smallest piece of code referred to as a <strong>unit</strong> (i.e method, class, component) that can be logically isolated in a system.</p>
<p>It is mainly focused on the functional correctness of standalone modules.</p>
<p>Unit testing consists of test cases that are used to check the business logic of your code.</p>
<h4><strong><span style="color: #0000ff;">Espresso</span></strong></h4>
<p class="p1"><span style="color: #008000;"><b>Espresso</b></span> is an open-source UI Testing framework made available by Google.</p>
<p class="p1">It is used to automate manual test cases for Android applications.</p>
<p class="p1">Espresso automatically <span style="color: #008000;"><b>synchronizes</b></span> your test actions with the UI of your application. The framework also ensures that your activity is started before the tests run and also lets the test wait until all observed background activities have finished.</p>
<p>If you are new to Espresso then refer to my previous article <strong><span style="color: #008000;"><a style="color: #008000;" href="https://c1ctech.com/android-espresso-testing-basics/">Android Espresso Testing Basics</a></span></strong> .</p>
<p class="p1">The main components of Espresso include the following:</p>
<ul class="ul1">
<li class="li1"><span style="color: #0000ff;"><b>Espresso</b></span> – Entry point to interactions with views (via <span style="color: #008000;"><b>onView() and onData()</b></span>).</li>
<li class="li1"><span style="color: #0000ff;"><b>ViewMatchers</b></span> –<span style="color: #008000;"><b>(Match a view)</b></span> Allows you to find view in the current view hierarchy.</li>
<li class="li1"><span style="color: #0000ff;"><b>ViewActions</b></span> – <span style="color: #008000;"><b>(Perform an action)</b></span>Allows you to perform an action on a View.</li>
<li class="li1"><span style="color: #0000ff;"><b>ViewAssertions</b></span> – <span style="color: #008000;"><b>(Assert and verify the result)</b></span>Check the state of the View to see if it reflects the expected state or behavior defined by the assertion.</li>
</ul>
<p>Now let&#8217;s start creating our CalculatorApp example.</p>
<h3><strong><span style="color: #000080;">Creating CalculatorApp</span></strong></h3>
<p>1 . In our CalculatorApp inside<span style="color: #0000ff;"><strong> main>;java</strong></span> we will create one java file <span style="color: #008000;"><strong>Calculation.Java</strong></span> in which we will define all the operations (ie. addition , subtraction, multiplication , division, percentage) which we want to perform on two numbers.</p>
<p><strong><span style="color: #0000ff;">Calculation.Java</span></strong></p>
<pre>package com.example.calculatorapp;

public class Calculation {

 public double addTwoNumbers(double a, double b) {
 return a + b;
 }

 public double subtractTwoNumbers(double a, double b) {
 return a - b;
 }

 public double multiplyTwoNumbers(double a, double b) {
 return a * b;
 }

 public double divideTwoNumbers(double a, double b) {
 return a / b;
 }

 public double calculatePercentage(double a) {
 return a / 100;
 }

}</pre>
<p>2 .To create Unit and UI tests cases for the operations defined in Calculation class we will create two classes:</p>
<ul>
<li><span style="color: #0000ff;"><strong>CalculationUnitTest</strong></span> under <span style="color: #008000;"><strong>test</strong></span> folder for writing Unit tests.</li>
<li><span style="color: #0000ff;"><strong>CalculationTest</strong></span> under <strong><span style="color: #008000;">androidTest</span></strong> folder for writing UI tests.</li>
</ul>
<p>3 .Now right-click on Calculation class and select <span style="color: #0000ff;"><strong>Generate..>;Tests..</strong></span></p>
<p><img class="alignnone wp-image-1452" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-21-15.02.20.png" alt="Screenshot 2020-01-21 15.02.20" width="549" height="313" /></p>
<p>4 .A new <span style="color: #008000;"><strong>Create Test</strong></span> window will appear as shown below:</p>
<p><img class="alignnone wp-image-1454" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-21-15.03.43.png" alt="Screenshot 2020-01-21 15.03.43" width="509" height="536" /></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p>5 .In above window provide Test class name and check all the methods which you want to use in your class and then click <strong><span style="color: #008000;">OK</span></strong>.</p>
<p>6 .Now choose the destination directory ie. <strong><span style="color: #008000;">androidTest</span></strong> or <strong><span style="color: #008000;">test</span></strong> in which you will add the above-defined class and then click <span style="color: #008000;"><strong>OK</strong></span>.</p>
<p><img class="alignnone wp-image-1455" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-21-15.05.03.png" alt="Screenshot 2020-01-21 15.05.03" width="523" height="546" /></p>
<p>After creating the <span style="color: #008000;"><strong>TestClass</strong></span> you are ready to start writing your test cases.</p>
<p>7 .<span style="color: #000080;"><strong>CalculationUnitTest</strong></span> consist of unit test cases for each operation defined with <strong><span style="color: #008000;">@Test</span></strong> notation.</p>
<p><strong><span style="color: #0000ff;">CalculationUnitTest.Java</span></strong></p>
<pre>package com.example.calculatorapp;

import org.junit.Test;

import static org.junit.Assert.*;


public class CalculationUnitTest {

 @Test
 public void addition_isCorrect() {
 assertEquals(5, new Calculation().addTwoNumbers(2,3),0);
 }


 @Test
 public void subtraction_isCorrect() {
 assertEquals(5, new Calculation().subtractTwoNumbers(8,3),0);
 }

 @Test
 public void multiplication_isCorrect() {
 assertEquals(24, new Calculation().multiplyTwoNumbers(8,3),0);
 }

 @Test
 public void division_isCorrect() {
 assertEquals(3, new Calculation().divideTwoNumbers(9,3),0);
 }

 @Test
 public void percentage_isCorrect() {
 assertEquals(0.03, new Calculation().calculatePercentage(3),0);
 }

}


</pre>
<p>Each test consist of <strong><span style="color: #008000;">assertEquals()</span></strong> which takes three inputs:</p>
<ul>
<li>expected value.</li>
<li>actual value to check against expected value.</li>
<li>delta ie. the maximum delta between expected and<br />
actual value for which both numbers are still<br />
considered equal.</li>
</ul>
<p>8 .To run all unit test cases right-click on <strong><span style="color: #008000;">CalculationUnitTest</span></strong> class and then select <strong><span style="color: #0000ff;">Run &#8216;CalculationUnitTest&#8217;</span></strong>.</p>
<p><img class="alignnone size-full wp-image-1457" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-21-17.40.34.png" alt="Screenshot 2020-01-21 17.40.34" width="1158" height="414" /></p>
<p>9 .<strong><span style="color: #000080;">CalculationTest</span></strong> consist of UI test cases for all the operations.</p>
<ul>
<li><strong><span style="color: #0000ff;">@RunWith</span> </strong>: specifies the behavior for our TestClass.To create an instrumented JUnit 4 test class, add the <strong><span style="color: #008000;">@RunWith(AndroidJUnit4.class)</span> </strong>annotation at the beginning of your test class definition.</li>
<li><strong><span style="color: #0000ff;">@Rule</span> </strong>: Provides functional testing of a single activity. UI can access the activity using the rule and thereby can access the resources etc.</li>
<li><span style="color: #0000ff;"><strong>@Test </strong></span>: The @Test annotation tells <span style="color: #008000;"><strong>JUnit</strong></span> that the <span style="color: #008000;"><strong>public void</strong></span> method to which it is attached can be run as a test case.</li>
</ul>
<p><strong><span style="color: #0000ff;">CalculationTest.Java</span></strong></p>
<pre>package com.example.calculatorapp;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
public class CalculationTest {

 @Rule
 public ActivityTestRule<;MainActivity>; activityScenarioRule
 = new ActivityTestRule<;>;(MainActivity.class);

 @Test
 public void addTwoNumbers() {
 onView(withId(R.id.btn_two)).perform(click());
 onView(withId(R.id.btn_plus)).perform(click());
 onView(withId(R.id.btn_three)).perform(click());
 onView(withId(R.id.btn_equalsto)).perform(click());
 onView(withId(R.id.edt_calculation)).check(matches(withText("5")));


 }

 @Test
 public void subtractTwoNumbers() {
 onView(withId(R.id.btn_eight)).perform(click());
 onView(withId(R.id.btn_minus)).perform(click());
 onView(withId(R.id.btn_three)).perform(click());
 onView(withId(R.id.btn_equalsto)).perform(click());
 onView(withId(R.id.edt_calculation)).check(matches(withText("5.0")));

 }

 @Test
 public void multiplyTwoNumbers() {
 onView(withId(R.id.btn_eight)).perform(click());
 onView(withId(R.id.btn_multiply)).perform(click());
 onView(withId(R.id.btn_three)).perform(click());
 onView(withId(R.id.btn_equalsto)).perform(click());
 onView(withId(R.id.edt_calculation)).check(matches(withText("24.0")));
 }

 @Test
 public void divideTwoNumbers() {
 onView(withId(R.id.btn_nine)).perform(click());
 onView(withId(R.id.btn_divide)).perform(click());
 onView(withId(R.id.btn_three)).perform(click());
 onView(withId(R.id.btn_equalsto)).perform(click());
 onView(withId(R.id.edt_calculation)).check(matches(withText("3.0")));
 }

 @Test
 public void calculatePercentage() {
 onView(withId(R.id.btn_three)).perform(click());
 onView(withId(R.id.btn_percentage)).perform(click());
 onView(withId(R.id.edt_calculation)).check(matches(withText("0.03")));
 }
}</pre>
<ul>
<li><span style="color: #008000;"><strong>onView()</strong></span> is used to select a view for testing.</li>
<li><span style="color: #008000;"><strong>withId()</strong></span> is used to locate the UI element.</li>
<li><span style="color: #008000;"><strong>perform(click())</strong></span> is used to perform click operation.</li>
<li><span style="color: #008000;"><strong>check(matches(withText()))</strong></span> is used to check whether the element is displayed with particular text or not.</li>
</ul>
<p>10 .To run all UI test cases right-click on <strong><span style="color: #008000;">CalculationTest</span></strong> class and then select <strong><span style="color: #0000ff;">Run &#8216;CalculationTest&#8217;</span></strong>.</p>
<p><img class="alignnone wp-image-1458" src="https://c1ctech.com/wp-content/uploads/2020/01/calculatorApp_UI_testing.gif" alt="calculatorApp_UI_testing" width="394" height="700" /></p>
<p> ;</p>


