Home » Mockito Tutorial | Mockito Framework Tutorial

Mockito Tutorial | Mockito Framework Tutorial

by Online Tutorials Library

Mockito Framework Tutorial

Mockito Tutorial

The Mockito tutorial provides basic and advanced concepts of the Mockito framework. Our Mockito tutorial is designed for both beginners and professionals. It will help you to learn and create unit tests using the Mockito framework.

Mockito is a mocking framework. It is a Java-based library used to create simple and basic test APIs for performing unit testing of Java applications. It can also be used with other frameworks such as JUnit and TestNG.

What is Unit testing?

Unit testing is a software testing technique in which individual components/parts of the software is tested, i.e., a group of computer programs, usage procedure, etc. Unit testing of an object is done during the development of an application or project. The aim of unit testing is to isolate a segment of code (unit) and verifies its correctness. A unit is referred to as an individual function or procedure (program). The developers usually perform it during testing.

What is Mocking?

Mocking is a process of developing the objects that act as the mock or clone of the real objects. In other words, mocking is a testing technique where mock objects are used instead of real objects for testing purposes. Mock objects provide a specific (dummy) output for a particular (dummy) input passed to it.

The mocking technique is not only used in Java but also used in any object-oriented programming language. There are many frameworks available in Java for mocking, but Mockito is the most popular framework among them.

To mock objects, you need to understand the three key concepts of mocking, i.e., stub, fake, and mock. Some of the unit tests involve only stubs, whereas some involve fake and mocks.

The brief description of the mocking concepts is given below:

  1. Stub: Stub objects hold predefined data and provide it to answer the calls during testing. They are referred to as a dummy object with a minimum number of methods required for a test. It also provides methods to verify other methods used to access the internal state of a stub, when necessary. Stub object is generally used for state verification.
  2. Fake: Fake are the objects that contain working implementations but are different from the production one. Mostly it takes shortcuts and also contains the simplified version of the production code.
  3. Mock: Mock objects act as a dummy or clone of the real object in testing. They are generally created by an open-source library or a mocking framework like Mockito, EasyMock, etc. Mock objects are typically used for behavior verification.

Need for mocking

Before using the Mocking technique, we should know the reasons for using mocking, which are as follows:

  • If we want to test a component that depends on the other component, but it is under development. It generally uses when working in a team and parts are divided between several team-mates. In this case, mocking plays an essential role in the testing of that component. Without mocking, we need to wait for the completion of the required elements for testing.
  • If the real components perform slow operations while dealing with database connections or another complex read/ write operation. Sometimes the database queries can take 10, 20, or more seconds to execute. In such cases, we require mock objects to perform testing, and it can be done via mocking.
  • If there is an infrastructure concern that makes the testing impossible. It is very similar to the first case. For example, when we create a connection to the database, some issues related to configurations occur. It requires mocking for creating mock components to provide unit testing.

What is Mockito?

Mockito is a Java-based mocking framework used for unit testing of Java application. Mockito plays a crucial role in developing testable applications. Mockito was released as an open-source testing framework under the MIT (Massachusetts Institute of Technology) License. It internally uses the Java Reflection API to generate mock objects for a specific interface. Mock objects are referred to as the dummy or proxy objects used for actual implementations.

The main purpose of using the Mockito framework is to simplify the development of a test by mocking external dependencies and use them in the test code. As a result, it provides a simpler test code that is easier to read, understand, and modify. We can also use Mockito with other testing frameworks like JUnit and TestNG.

The Mockito framework was developed by upgrading the syntax and functionalities of

EasyMock framework. It was developed by a team of developers consisting of Szczepan Faber, Brice Dutheil, Rafael Winterhalter, Tim van der Lippe, and others. The stable or latest version of Mockito is version 3.0.6 was released in August 2019.

Benefits of Mockito

Below are given some benefits of the Mockito framework:

Benefits of Mockito

  • No handwriting: In Mockito, there is no requirement for writing your mock objects.
  • No handwriting: In Mockito, there is no requirement for writing your mock objects.
  • Safe refactoring: While renaming the method name of an interface or interchanging the parameters do not change the test code, as mock objects are created at runtime.
  • Exception support: It supports the exception. In Mockito, the stack trace is used to find the cause of the exception.
  • Annotation support: It creates mock objects using annotations like @Mock.
  • Order support: It provides a check on the order of the method calls.

Prerequisites

Before learning the concepts of the Mockito framework, you should have hands-on experience in Java programming and some basic understanding of the JUnit framework.

Audience

Our Mockito Tutorial is designed to help beginners and professionals.

Problem

We assure you that you will not find any problem with our Mockito Tutorial. But if there is any query or problem, please post the same in our contact form.


Next TopicMethods of Mockito

You may also like