Author

Author- Ram Ranjeet Kumar

Wednesday, August 16, 2023

Mockito Framework


Introduction
  • Mockito is an open-source testing framework for Java released under the MIT(Massachusetts Institute of Technology)  License. 
  • Mockito is a Java-based mocking framework used for unit testing of Java applications. 
  • Mockito plays a crucial role in developing testable applications.
  • Mockito 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.

Here's a simple explanation with an example:
Imagine you're building a car. Your car has an engine, and the engine needs to be tested. But, testing the engine might involve other complex parts like the transmission and fuel system. Instead of building an entire car just to test the engine, you could use a mock engine that simulates the real engine's behavior.

In coding terms, Mockito lets you create these "mock" objects to stand in for real components. You can use these mock objects to check if certain interactions between parts of your code are happening correctly.

Let's say you have a class called `Car` that depends on an `Engine` class. You want to test the `Car` class without worrying about the `Engine` class's actual behavior. Here's how you could do it with Mockito:

import static org.mockito.Mockito.*;
// Imagine you have these classes
class Engine {
    public String start() {
        return "Vroom!";
    }
}

class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }

    public String startCar() {
        return engine.start();
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a mock engine
        Engine mockEngine = mock(Engine.class);

        // Set up the mock's behavior
        when(mockEngine.start()).thenReturn("Mock Vroom!");

        // Create a car using the mock engine
        Car car = new Car(mockEngine);

        // Test the car's behavior
        String carStartSound = car.startCar();
        System.out.println(carStartSound); // Output: "Mock Vroom!"
    }
}

In this example, `mock(Engine.class)` creates a mock object that behaves like an `Engine` but doesn't have the real engine's functionality. The `when(mockEngine.start()).thenReturn("Mock Vroom!")` line sets up the mock engine to return `"Mock Vroom!"` when its `start()` method is called.

By using Mockito, you can test the `Car` class's behavior without relying on the real `Engine` class, making your tests faster, more focused, and easier to maintain.


What is the difference between Mockito and JUnit?
  • JUnit and Mockito are widely used testing frameworks in the Java ecosystem, each serving distinct purposes. 
  • JUnit is a unit testing framework that focuses on writing and executing test cases for individual units of code. 
  • Mockito, on the other hand, is a mocking framework specifically designed to create mock objects for testing purposes. 
  • While JUnit focuses on testing individual units of code, Mockito specializes in managing dependencies and mocking external interactions.
  • In summary, JUnit and Mockito are two powerful and complementary testing frameworks for Java applications.

Benefits of Mockito
Mockito is a popular Java mocking framework that allows you to create mock objects for unit testing. Some of the benefits of Mockito include:
  • No handwriting: In Mockito, there is no requirement for writing your mock objects.
  • Safe refactoring: While renaming the method name of an interface or reordering parameters will not break the test code as Mocks are created at run time.
  •  Annotation support: It supports creating mocks using various annotations.
  •  Return value support: Supports return values.
  •  Exception support: It supports exceptions.


No comments:

Post a Comment