Unit Testing in Android using Mockito - GeeksforGeeks

26 Nov, 2021
Most classes have dependencies, and methods frequently delegate work to other methods in other classes, which we refer to as class dependencies. If we just utilized JUnit to unit test these methods, our tests would likewise be dependent on them. All additional dependencies should be independent of the unit tests. As a result, we simply mock the dependency class and test the Main Class. Mockito is a mocking framework with a delicious flavor. It has a clean and simple API that allows you to construct beautiful tests. The tests in Mockito are very readable and provide clear verification errors, so you won’t get a hangover. Now, let’s look at an example of how to use mockito.
We begin by adding the dependency to the app. file gradle:
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.19.0'
Image #1: Creating the new project
object Operators {
    add(m: Int, n: Int): Int = m + n
    subtract(n: Int, m: Int): Int = n - m
    multiply(c: Int, a: Int): Int = c * a
    divide(l: Int, d: Int): Int = l / d
}
Also below is the calculator class:
Kotlin
class GfGCalculator(private val operators: Operators) {    fun addTwoNumbers(ab: Int, ba: Int): Int = operators.add(ab, ba)    fun subtractTwoNumbers(ac: Int, bc: Int): Int = operators.subtract(ac, bc)    fun multiplyTwoNumbers(ad: Int, bd: Int): Int = operators.multiply(ad, bd)    fun divideTwoNumbers(aa: Int, ba: Int): Int = operators.divide(aa, ba)}
The primary function Object() { [native code] } of Calculator takes operator object as an argument. As a result, the functions in the Calculator class return operator function as a return param. Now, Let’s get started testing the Calculator Class. We’ll build a package called calculator in the test folder, just like we did in the java folder.
Image #2: The calculator test.
Kotlin
@RunWith(MockitoJUnitRunner::class)class CalculatorTestGfG {}
We haven’t developed OperatorsTest because we simply need to test CalculatorTest here. MockitoJUnitRunner::class is annotated here, which signifies it offers a Runner to run the test. We’ll now set up the calculator class.
Kotlin
@RunWith(MockitoJUnitRunner::class)class CalculatorTestGfG {    lateinit var calc: Calculator    @Before    fun someSetup() {        calc = Calculator(/** your operators here **/)    }}
The operators must be passed in construct. As a result, we will not create an operator object as, we want to run the test in isolation so that if the Operators crash, it won’t affect the CalculatorTest test. We only need to use the Operator class’s invoke methods to communicate with the outside world. Also, @Before implies that we must set up the dependency even before running the test.
Kotlin
@RunWith(MockitoJUnitRunner::class)class CalculatorTestGfG {    @Mock    lateinit var ops: Operators    lateinit var calc: Calculator    @Before    fun someSetup() {        calc = Calculator(operators)    }}
Using the @Mock annotation, we can mock any class in Mockito. By mocking a specific class, we create a mock object of that class. Operators are mocked in the above code to provide A Calculator with a dependency. Now we’ll make two variables, a and b, with the values 12,21, and call them.
calc.addTwoNumbers(ab, ba)
We’ll only use, to see if the right function was called or not from the mocked class.
verify(operators).add(ab, ba) 
Verify indicates that you want to see if a particular method of a mock object has been called or not.
Image #3: The context menu
This will execute the test and produce an output indicating if the test passed or failed. The test passed in our case since we used the right function. Our test will fail because we are calling addNumbers from the Calculator class, then subtracting from fake Operators.
Image #4: Error running the test
This is how we can utilize Mockito in our app to perform unit testing. Now, in order to test all of the functions, write and run the code below
Kotlin
package com.geeksforgeeks.calc  import org.junit.Beforeimport org.junit.Testimport org.junit.runner.RunWithimport org.mockito.Mockimport org.mockito.Mockito.verifyimport org.mockito.junit.MockitoJUnitRunner  @RunWith(MockitoJUnitRunner::class)class CalculatorTest {    @Mock    lateinit var ops: Operators    lateinit var calc: Calculator    @Before    fun someSetup() {        calc = Calculator(ops)    }      @Test    fun givenValidInput_whenAdd_shouldCallAddOperator() {        val aa = 11        val ba = 21        calculator.addTwoNumbers(aa, ba)        verify(operators).add(aa, ba)    }      @Test    fun givenValidInput_whenSubtract_shouldCallSubtractOperator() {        val ac = 11        val bc = 21        calc.subtractTwoNumbers(ac, bc)        verify(ops).subtract(ac, bc)    }          @Test    fun givenValidInput_whenMultiply_shouldCallMultiplyOperator() {        val av = 11        val bv = 21        calc.multiplyTwoNumbers(av, bv)        verify(ops).multiply(av, bv)    }          @Test    fun givenValidInput_whenDivide_shouldCallDivideOperator() {        val ap = 11        val bp = 21        calc.divideTwoNumbers(ap, bp)        verify(ops).divide(ap, bp)    }}
GeekTip: Mockito cannot directly test final/static classes because all Kotlin classes are final. We need to set up mockito-extensions in order to run tests on final classes.
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Android Listview in Java with Example
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
How to Add Image to Drawable Folder in Android Studio?
How to Change the Background Color After Clicking the Button in Android?
How to Retrieve Data from the Firebase Realtime Database in Android?
GridView in Android with Example
ImageView in Android with Example