Open In App

Unit Testing in Android using Mockito

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Example

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.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import 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.



Last Updated : 26 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads