{"id":445,"date":"2024-06-26T15:09:24","date_gmt":"2024-06-26T15:09:24","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=445"},"modified":"2026-02-05T12:06:56","modified_gmt":"2026-02-05T12:06:56","slug":"how-to-unit-testing-with-mockito-in-android","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/how-to-unit-testing-with-mockito-in-android\/","title":{"rendered":"How to Perform Unit Testing with Mockito in Android"},"content":{"rendered":"\n<p>Unit testing for Android allows us to test a specific section of code to ensure that it meets our requirements. The code fragment is pushed to the repository to be combined with the current code if it passes. If it doesn&#8217;t work, the developers correct the problem and try again until it does.<\/p>\n\n\n\n<p>One of the most useful aspects of developing Android apps is unit testing, which helps us save a tonne of time and money on the project as a whole.<\/p>\n\n\n\n<p>Small code components, such as methods or classes, are usually the subject of these tests, which are conducted separately from the application as a whole. Developers should think about including the following three tests in their test suites:<br><\/p>\n\n\n\n<p><strong>1. UI Tests: <\/strong>These tests validate the user interface of an application. There&#8217;s Espresso.<\/p>\n\n\n\n<p><strong>2. Instrumented Tests: <\/strong>These tests employ APIs from the Android framework to run on real or emulated Android devices to verify user behaviors like button clicks. Espresso &amp; AndroidX Tests are frequently used for the same purpose.<\/p>\n\n\n\n<p><strong>3. Unit Tests:<\/strong> Unit tests are crucial for ensuring that every operation and process in the application is tested. The most popular tools for unit testing are Hamcrest, Mockito, and JUnit.<\/p>\n\n\n\n<p>Verifying the correctness of code is the primary goal of unit testing, which aims to fix issues early in the development cycle. This blog will explain Mockito and walk through how to use it for unit testing in Android applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps: Unit Testing Using Mockito in Android<br><\/h2>\n\n\n\n<p>Let\u2019s take a simple example of computations like addition, subtraction, etc to understand the Mockito framework.<\/p>\n\n\n\n<p>We will create a file <em>ComputationActivity.kt<\/em> to display all computations upfront. For managing all operations we will create object class Operations.kt which will be passed to <em>ComputationActivity<\/em> class as a param in the primary constructor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">To Add Mockito to Your Project (Using Android Studio and Gradle):<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>build.gradle.kts(app level)<\/strong>\ntestImplementation (\"junit:junit:4.13.2\")\ntestImplementation (\"org.mockito:mockito-core:2.25.0\")\ntestImplementation (\"org.mockito:mockito-inline:2.13.0\")\n\n<strong>build.gradle(app level) <\/strong>\ntestImplementation 'junit:junit:4.13.2'\ntestImplementation 'org.mockito:mockito-core:2.25.0'\ntestImplementation 'org.mockito:mockito-inline:2.13.0'<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Folder structure for adding<strong> ExampleUnitTest.kt file<\/strong> for testing <strong>MainActivity.kt<\/strong> class.<\/li>\n<\/ul>\n\n\n\n<p>By default, <em>module-name\/src\/test<\/em> will be having source files for local unit tests.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"624\" height=\"213\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/module-name-src-test.png\" alt=\"Module Name Src Test \" class=\"wp-image-449\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/module-name-src-test.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/module-name-src-test-300x102.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p>Now, we will add test functions in our <strong>ExampleUnitTest.kt <\/strong>file<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>@RunWith<\/strong> \u2013 we have annotated with MockitoJUnitRunner::class which means it provides the Runner to run the test.<\/li>\n\n\n\n<li><strong>@Mock<\/strong>\u2013 Using <strong>@Mock<\/strong> annotation we can mock any class. Mocking any class is nothing but to create a mock object of a particular class.<\/li>\n<\/ul>\n\n\n\n<p>Here, Operators is a object which has set of functions of calculator operations<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"624\" height=\"265\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/set-of-functions-of-calculator-operations.png\" alt=\"set of functions of calculator operations\" class=\"wp-image-450\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/set-of-functions-of-calculator-operations.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/set-of-functions-of-calculator-operations-300x127.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p>and Calculator is a class,<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"624\" height=\"255\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/mockito-example.png\" alt=\"Mockito example\" class=\"wp-image-451\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/mockito-example.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/mockito-example-300x123.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p>Here, Calculator is mockito example that takes the operator object as a parameter in the primary constructor. So, we return operator function as return param for the functions in the Calculator class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Let&#8217;s Start Testing the Calculator Class,<\/h2>\n\n\n\n<p>Inside the test folder, we will create a package called calculator same as we did in the java folder.<br>In that, we will create only one class called CalculatorTest.kt.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"624\" height=\"171\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/calculatortest-kt.png\" alt=\"CalculatorTest kt\" class=\"wp-image-452\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/calculatortest-kt.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/calculatortest-kt-300x82.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p>Here, you can see we have not created OperatorsTest as we have to test only CalculatorTest here.<\/p>\n\n\n\n<p>So, in Calculator Test,<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"624\" height=\"221\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/calculator-test.png\" alt=\"Calculator Test\" class=\"wp-image-453\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/calculator-test.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/calculator-test-300x106.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p>Here, we have annotated with <strong><em>MockitoJUnitRunner::class <\/em><\/strong>that means it provides the Runner to run the test.<\/p>\n\n\n\n<p>Now, we will setup the calculator class<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"624\" height=\"189\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/setup-calculator-class.png\" alt=\"setup calculator class\" class=\"wp-image-454\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/setup-calculator-class.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/setup-calculator-class-300x91.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p>Here, in construct we need to pass the operators. So, we won&#8217;t create an object of the operator as,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We want to perform the test in isolation so that even if the Operators crashes, it should not impact the test being performed in CalculatorTest.<\/li>\n\n\n\n<li>We have to just have the invoke methods of the Operator class and do external communication.<\/li>\n<\/ul>\n\n\n\n<p>Also, <strong>@Before <\/strong>means, that even before performing the test we need to setup the dependency.<\/p>\n\n\n\n<p>So, here we have to <strong>mock <\/strong>the operator like,<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignleft size-full\"><img decoding=\"async\" width=\"624\" height=\"237\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/mock.png\" alt=\"mock\" class=\"wp-image-455\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/mock.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/mock-300x114.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In Mockito, we mock any class using <strong>@Mock <\/strong>annotation.<\/p>\n\n\n\n<p>By Mocking any class, we are creating an mock object of that speicifc class. In the above code, Operators is mocked to provide dependency for Calculator class. Now, lets perform some test now,<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignleft size-full\"><img decoding=\"async\" width=\"620\" height=\"201\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/creating-an-mock-object.png\" alt=\"creating an mock object\" class=\"wp-image-456\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/creating-an-mock-object.png 620w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/creating-an-mock-object-300x97.png 300w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here, to perform tests we need to annotate the function with <strong>@Test.<\/strong><\/p>\n\n\n\n<p>Now, we will create two variables, a and b with values 10,20 and then we will call,<\/p>\n\n\n\n<p><strong>calculator.addTwoNumbers(a, b)<\/strong><\/p>\n\n\n\n<p>and to verify if the correct function was called or not from the mocked class, we will just use,<\/p>\n\n\n\n<p><strong>verify(operators).add(a, b)<\/strong><\/p>\n\n\n\n<p>Here, verify means that you want to check if a certain method of a mock object has been called or not.<\/p>\n\n\n\n<p>Now, to run the test we can click on the function like,<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"624\" height=\"344\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/certain-method-mock-object.png\" alt=\"certain method-mock-object \" class=\"wp-image-457\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/certain-method-mock-object.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/certain-method-mock-object-300x165.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p>Now, to fail the case, just replace<\/p>\n\n\n\n<p><strong>verify(operators).subtract(a, b)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"624\" height=\"352\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/verify-operators-subtract.png\" alt=\"verify(operators) subtract\" class=\"wp-image-459\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/verify-operators-subtract.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/verify-operators-subtract-300x169.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p>Now, to run test on all the functions,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.app.mocktest.Calculator\nimport com.app.mocktest.Operators\nimport org.junit.Before\nimport org.junit.Test\nimport org.junit.runner.RunWith\nimport org.mockito.Mock\nimport org.mockito.Mockito.verify\nimport org.mockito.junit.MockitoJUnitRunner\n\n@RunWith(MockitoJUnitRunner::class)\nclass CalculatorTest {\n   @Mock\n   lateinit var operators: Operators\n\n   lateinit var calculator: Calculator\n\n   @Before\n   fun onSetup() {\n       calculator = Calculator(operators)\n   }\n\n\n   @Test\n   fun givenValidInput_whenAdd_shouldCallAddOperator() {\n       val a = 10\n       val b = 20\n       calculator.addTwoNumbers(a, b)\n       verify(operators).add(a, b)\n\n   }\n\n   @Test\n   fun givenValidInput_whenSubtract_shouldCallSubtractOperator() {\n       val a = 10\n       val b = 20\n       calculator.subtractTwoNumbers(a, b)\n       verify(operators).subtract(a, b)\n\n   }\n   @Test\n   fun givenValidInput_whenMultiply_shouldCallMultiplyOperator() {\n       val a = 10\n       val b = 20\n       calculator.multiplyTwoNumbers(a, b)\n       verify(operators).multiply(a, b)\n\n   }\n   @Test\n   fun givenValidInput_whenDivide_shouldCallDivideOperator() {\n       val a = 10\n       val b = 20\n       calculator.divideTwoNumbers(a, b)\n       verify(operators).divide(a, b)\n\n   }\n}\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"624\" height=\"263\" src=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/run-test-on-all-the-functions.png\" alt=\"run test on all the functions\" class=\"wp-image-460\" srcset=\"https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/run-test-on-all-the-functions.png 624w, https:\/\/www.cmarix.com\/qanda\/wp-content\/uploads\/2024\/06\/run-test-on-all-the-functions-300x126.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This blog discussed using Mockito for unit testing Android applications. Mocking is a potent tool for creating efficient Android unit tests. It enables you to independently verify the behavior of distinct code units, resulting in more dependable and manageable tests.<\/p>\n\n\n\n<p>Because unit testing identifies defects early in the development process and makes refactoring and code modifications easier, Android applications need to be reliable, maintainable, and stable. If you also want to use Mockito development for Android unit testing, <a href=\"https:\/\/www.cmarix.com\/hire-android-developers.html\">hire Android developers<\/a> with CMARIX.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing for Android allows us to test a specific section of code to ensure that it meets our requirements. The code fragment is pushed to the repository to be combined with the current code if it passes. If it doesn&#8217;t work, the developers correct the problem and try again until it does. One of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":501,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,9],"tags":[],"class_list":["post-445","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile","category-android"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":9,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/445\/revisions\/466"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/501"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}