2 Minutes To Code (β Version)

Mockito - 2 minute tutorial

How to create a mock object

This example shows how to create a dummy or mock for an object.

A mock object of Calendar class is created by using the method mock(...) of class org.mockito.Mockito.

The usage of mock objects is explained in examples on stubbing method in the next few pages.
package com.techfundaes.mockitoBag;

import java.util.Calendar;

import static org.mockito.Mockito.*;

public class CreateMocks
{
	public static void main(String[] args)
	{
		Calendar mockedCalendar = mock(Calendar.class);
		when(mockedCalendar.get(Calendar.YEAR)).thenReturn(2020);
		
		System.out.println(mockedCalendar.get(Calendar.YEAR));
	}
}