2 Minutes To Code (β Version)

Mockito - 2 minute tutorial

How to stub method with hardcoded arguments

This example explains how to use mock objects by stubbing method behaviour.

Here, a mock object of java.util.List class is hard-wired to return a string object "target" whenever the get method is invoked on it with argument 0.
package com.techfundaes.mockitoBag;

import static org.mockito.Mockito.*;

import java.util.List;

public class CustomiizeMethodBehaviour
{
	public static void main(String[] args)
	{
		List myMockedList = mock(List.class); 
		when(myMockedList .get(0)).thenReturn("target");
		
		System.out.println(myMockedList .get(0));
	}
}