Unit Testing in Android using Mockito
Last Updated :
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.
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 projectobject 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.
Similar Reads
Unit Testing in Android using JUnit
Unit testing is done to ensure that developers write high-quality and errorless code. It is advised to write Unit tests before writing the actual app, you will write tests beforehand and the actual code will have to adhere to the design guidelines laid out by the test. In this article, we are using
5 min read
Testing Room Database in Android using JUnit
In this article, we are going to test the Room Database in android. Here we are using JUnit to test our code. JUnit is a âUnit Testingâ framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains an
5 min read
Why is Unit Testing Harder in OOP?
Unit testing is a crucial aspect of software development, serving as the first line of defense against bugs and ensuring the reliability and maintainability of code. Table of Content Steps of Unit TestingChallenges of Unit Testing in OOPStrategies for Overcoming ChallengesExample of unit testingConc
9 min read
UI Testing with Espresso in Android Studio
UI testing is the process of testing the visual elements of an application to ensure whether they appropriately meet the anticipated functionality. Verifying the application manually whether it works or not is a time taking and tiring process, but using espresso we can write automated tests that run
6 min read
Unit Testing in Devops
In today's rapidly evolving digital world developers are eager to deliver high-quality software at a faster pace without any errors, issues, or bugs, to make this happen the importance of Unit testing in DevOps continues to grow rapidly. unit testing helps the DevOps team to identify and resolve any
9 min read
Dart - Unit Testing
Unit Testing is a process of testing individual components of a software/application separately, to ensure that each component is functioning as intended. It provides multiple advantages such as - Ability to test out individual components without running the whole software/application.Pinpoint erro
3 min read
Mocking a RestTemplate in Spring
The Spring Boot framework provides many features for testing purposes also a lot of Spring annotations are available for testing the Application performance. The Spring is a common practice for testing applications that make HTTP requests without hitting the network. This helps in isolating your tes
7 min read
How to Post Data to API using Retrofit in Android?
We have seen reading data from API in our Android app in Android Studio. For reading data from API, we use GET request to read our data which is in JSON format. In this article, we will take a look at adding data to REST API in our Android App in Android Studio. What we are going to build in this ar
6 min read
Unit Testing in R Programming
The unit test basically is small functions that test and help to write robust code. From a robust code we mean a code which will not break easily upon changes, can be refactored simply, can be extended without breaking the rest, and can be tested with ease. Unit tests are of great use when it comes
5 min read
Introduction to Retrofit in Android
Retrofit is a type-safe HTTP client for Android, developed by Square. It simplifies network operations by allowing developers to define REST API interactions using Java/Kotlin interfaces. It supports various request methods like GET, POST, PUT, DELETE, and PATCH while enabling seamless integration w
5 min read