Mockito Basic Concepts

Introduction

In this chapter, we will cover the basic concepts of Mockito. Understanding these concepts will help you use Mockito effectively to create and manage mock objects in your tests.

Mock Objects

What are Mock Objects?

Mock objects are fake versions of real objects. They are used in tests to simulate the behavior of real objects. By using mock objects, you can isolate the code you are testing from its dependencies.

Creating Mock Objects

You can create mock objects using the Mockito.mock() method. Here’s an example:

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

public class ExampleTest {

    @Test
    void testMock() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);
    }
}

Stubbing

What is Stubbing?

Stubbing is the process of defining what mock objects should do when their methods are called. This allows you to specify the behavior of mock objects in your tests.

Stubbing Methods

You can stub methods using the when(...).thenReturn(...) syntax. Here’s an example:

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ExampleTest {

    @Test
    void testStubbing() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Stub the add method
        when(calculator.add(2, 3)).thenReturn(5);

        // Use the mock object
        assertEquals(5, calculator.add(2, 3));
    }
}

Verification

What is Verification?

Verification is the process of checking whether certain methods were called on the mock objects during the test. This helps ensure that your code interacts with its dependencies as expected.

Verifying Interactions

You can verify interactions using the verify() method. Here’s an example:

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

public class ExampleTest {

    @Test
    void testVerification() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Use the mock object
        calculator.add(2, 3);

        // Verify that the add method was called
        verify(calculator).add(2, 3);
    }
}

Key Points

  1. Mock Objects: Fake versions of real objects used to simulate their behavior.
  2. Stubbing: Defining the behavior of mock objects when their methods are called.
  3. Verification: Checking if certain methods were called on the mock objects during the test.

Example: Calculator Class and Tests

Calculator.java

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public double multiply(double a, double b) {
        return a * b;
    }

    public double divide(double a, double b) {
        if (b == 0) {
            throw new IllegalArgumentException("Division by zero");
        }
        return a / b;
    }
}

CalculatorTest.java

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {

    @Test
    void testAdd() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Stub the add method
        when(calculator.add(2, 3)).thenReturn(5);

        // Use the mock object
        int result = calculator.add(2, 3);

        // Assert the result
        assertEquals(5, result);

        // Verify the interaction
        verify(calculator).add(2, 3);
    }

    @Test
    void testSubtract() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Stub the subtract method
        when(calculator.subtract(3, 2)).thenReturn(1);

        // Use the mock object
        int result = calculator.subtract(3, 2);

        // Assert the result
        assertEquals(1, result);

        // Verify the interaction
        verify(calculator).subtract(3, 2);
    }

    @Test
    void testMultiply() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Stub the multiply method
        when(calculator.multiply(2.0, 3.0)).thenReturn(6.0);

        // Use the mock object
        double result = calculator.multiply(2.0, 3.0);

        // Assert the result
        assertEquals(6.0, result, 0.001);

        // Verify the interaction
        verify(calculator).multiply(2.0, 3.0);
    }

    @Test
    void testDivide() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Stub the divide method
        when(calculator.divide(6.0, 3.0)).thenReturn(2.0);

        // Use the mock object
        double result = calculator.divide(6.0, 3.0);

        // Assert the result
        assertEquals(2.0, result, 0.001);

        // Verify the interaction
        verify(calculator).divide(6.0, 3.0);
    }

    @Test
    void testDivideByZero() {
        // Create a mock object of the Calculator class
        Calculator calculator = mock(Calculator.class);

        // Stub the divide method to throw an exception
        when(calculator.divide(1.0, 0)).thenThrow(new IllegalArgumentException("Division by zero"));

        // Assert the exception
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
            calculator.divide(1.0, 0);
        });

        assertEquals("Division by zero", exception.getMessage());

        // Verify the interaction
        verify(calculator).divide(1.0, 0);
    }
}

Conclusion

Understanding the basic concepts of Mockito, such as mock objects, stubbing, and verification, is essential for writing effective tests. By using these concepts, you can create reliable and maintainable tests that help ensure your code works as expected.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top