This is an HelloWorld tutorial about Mockito framework. In this tutorial we will show a basic example of Mockito, and how to verify some assertions on Mock objects.

Mocking provides a straightforward way to test the functionality of a class in isolation. As a matter of fact, you can simulate integration components such as database connections with Java components and still test a functionality. The component that simulates your service is called a Mock object and it returns a sample data corresponding to some dummy input passed to it.

Behind the hoods, Mockito facilitates creating mock objects by using Java Reflection in order to create mock objects for a given interface. Mock objects are just proxies for actual implementations.

Let's see a basic example of a Customer Service, defined by this interface:

package com.testingjava;

public interface CustomerService
{

    public int save(Customer customer);
}

Here is the simple model Class upon the CustomerService works:

package com.testingjava;

public class Customer {
    String name;
    String surname;

    public Customer(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public Customer() {
    }
}

And here is the actual application which does the functionality:

package com.testingjava;

import java.util.ArrayList;
import java.util.List;

public class CustomerApplication
{
    CustomerService customerService;


    public void setCustomerService(CustomerService customerService){
        this.customerService = customerService;
    }
    List customerList = new ArrayList();

    public int save(Customer customer) {
        customerList.add(customer);
        System.out.println("Saved customer");
        return customerList.size();
    }
}

In order to test our application we will use JUnit in combination with Mockito. Therefore the following libraries need to be included in your project:

  <dependencies>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>2.23.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

And now,let's code the Test class:

package com.testingjava;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class AppTest {
 
    @InjectMocks
    CustomerApplication customerService = new CustomerApplication();
   
    @Mock
    CustomerService mock;


    @Test
    public void saveTest() {


        Customer customer = new Customer("john", "smith");
        when(mock.save(customer)).thenReturn(1);
        Assert.assertEquals(mock.save(customer), 1);
        verify(mock, times(1)).save(customer);

    }
}

First off, we have used the @InjectMocks annotation to initialize and inject the mock object. Next, we use the @Mock annotation to create the mock object to be injected.

We have then added the behavior of CustomerService to add a new Customer. We have tested the functionality, asserting that the CustomerList saved equals to 1. Finally, we have used the verify's Mock method to verify that the save method has been invoked exactly one time.

Source code for this example: https://github.com/fmarchioni/testingjava/tree/master/mockito/hello-world

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials