Unit testing with Mockito Framework

Reading Time: 3 minutes
Unit testing with mockito

In this blog, we are going to learn about how we can use mockito framework to perform unit testing. Mockito framework provides us several methods which we can use to perform unit test.

What is Unit Testing

Unit testing is a software testing technique in which we only test a single component of the software. We ignore the output of the dependent module and test only the implementation of the current module. Developers perform unit testing during the development of an application.

What is Mocking

Mocking is a process to develop a cloned object of the real object. In other words, mocking is a testing technique in which we use cloned objects instead of real objects to perform unit testing. To ignore the implementation of the dependent module, we mock the objects of those modules and provide the dummy output. The mocking technique can be used in any object oriented programming language.

An Introduction to Mockito Framework

Mockito is a java based mocking framework which is used to perform unit testing. It is an open source testing framework which uses the Java Reflection API to generate mock objects. The main objective of Mockito is to simplify the development of a test by mocking external dependencies. Mockito can also be used with other test frameworks like Junit and TestNG.

Below is an example of how to perform unit testing using mockito:-


We need to add 2 dependencies in our build.sbt file. First is the scalatest which we use to perform any type of testing and second is mockito which we use to perform unit testing.

name := "Unit-Testing"

version := "0.1"

scalaVersion := "2.13.4"

libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.2.3" % Test,
  "org.mockito" %% "mockito-scala" % "1.5.12" % Test

Person model

package com.knoldus.model

case class Person(firstName: String,
                  lastName: String,
                  age: Int,
                  emailId: String)

PersonAge Class

This class has a method personIsSeniorCitizen() which checks that whether a person is a senior citizen or not.

package com.knoldus.example

class PersonAge() {
  def personIsSeniorCitizen(age: Int): Boolean = {
    if(age > 60) true else false

PersonValidity Class

This is the class which checks that whether a person is valid or not. So a person is valid if he/she is a senior citizen. So this class is dependent on PersonAge class.

package com.knoldus.example

import com.knoldus.model.Person

class PersonValidity(personAge: PersonAge) {

  def personIsValid(person: Person) : Boolean = {
    if(personAge.personIsSeniorCitizen(person.age)) true else false

We need to perform unit testing on PersonValidity class. So to perform unit testing, we need to ignore the implementation of personIsSeniorCitizen() by creating a mock object and providing a dummy output.


package com.knoldus.example

import com.knoldus.model.Person
import org.mockito.MockitoSugar.{mock, when}
import org.scalatest.flatspec.AnyFlatSpec

class PersonValidityTest extends AnyFlatSpec {
  val samplePerson : Person = Person("Bhavya","Verma",24,"bhavya.verma@knoldus.com")
  val mockedPersonAge = mock[PersonAge]

  "Person" should "be valid" in {
    val personValidity = new PersonValidity(mockedPersonAge)
    when(mockedPersonAge.personIsSeniorCitizen(samplePerson.age)) thenReturn(true)

  "Person" should "be invalid" in {
    val personValidity = new PersonValidity(mockedPersonAge)
    when(mockedPersonAge.personIsSeniorCitizen(samplePerson.age)) thenReturn(false)


As we can see that we have mocked the PersonAge class and have provided the dummy output by using when() method. If we change the age of Person then it will not affect the testing of PersonValidity, as we are not concerned with the implementation of PersonAge class. We are providing a dummy output which is not going to be affected by changing the values of Person object.

Checkout more fun blogs on Unit testing on Knoldus Blogs.




Written by 

Bhavya Verma is a Software Consultant at Knoldus. An avid Scala programmer, he is recognised as a good team player, dedicated and responsible professional, and a technology enthusiast. He has good time management skills. His hobbies include playing badminton, watching movies and travelling.