Learn Unit Testing in Android by building a sample application
<p>In the last article, I listed out the <a href="https://ayusch.com/unit-testing-and-why-should-you-start-writing-unit-tests/" rel="noopener ugc nofollow" target="_blank">benefits of Unit Testing your applications</a>. In this tutorial, we’ll take a look at how to begin Unit Testing your Android Applications.</p>
<p>If you haven’t checked out the previous article on <a href="https://ayusch.com/unit-testing-and-why-should-you-start-writing-unit-tests/" rel="noopener ugc nofollow" target="_blank">why you should unit test your android app</a>, then you must take a quick look at it before moving ahead with this one.</p>
<p>Let’s get started!!</p>
<h1>Install the dependencies</h1>
<p>Place these dependencies in your app level build.gradle file:</p>
<pre>
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'</pre>
<ul>
<li><strong>Junit</strong>: It is a “Unit Testing” framework for Java Applications. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After etc.</li>
<li><strong>Mockito</strong>: Mockito mocks (or fakes) the dependencies required by the class being tested. It provides annotations such as @Mock.</li>
</ul>
<h1>Create a sample app:</h1>
<blockquote>
<p><strong><em>**IMPORTANT**</em></strong></p>
<p><em>Unit Tests are generally written before writing the actual application. But for the sake of explanation in this article, I am creating a sample app before writing Unit Tests.</em></p>
<p><strong><em>Unit Testing</em></strong><em> is done to ensure that the developer would be unable to write low quality/erroneous code. It makes sense to write Unit Tests before writing the actual app as then you wouldn’t have a bias towards the success of your tests, you will write tests beforehand and the actual code will have to adhere to the design guidelines laid out by the test.</em></p>
</blockquote>
<p>Now, lets create our sample app.</p>
<p>We’ll be creating a simple app, whose sole purpose would be to get user data from input fields and save it using in a shared preference file</p>
<p><a href="https://medium.com/mindorks/learn-unit-testing-in-android-by-building-a-sample-application-23ec2f6340e8">Visit Now</a></p>