2 Minutes To Code (β Version)

Easymock - 2 minute tutorial

How to define mock behaviour for hardcoded arguments

After creating mock objects, you need to define behaviour of mock objects. This is done by using methods expect(...) and andReturn(...) of class org.easymock.EasyMock. After defining these methods, the replay(...) method must be called on the mocked object before calling the stubbed methods.

The example defines the behaviour of mock object when the method get(0) is called on it.
package com.techfundaes.easymockBag;

import static org.easymock.EasyMock.*;

import java.util.List;

public class DefineMockBehaviourSimple
{
	public static void main(String[] args)
	{
		List myList = createMock(List.class);
		
		expect(myList.get(0)).andReturn("abc");
		
		replay(myList);
	
		System.out.println(myList.get(0));
	}
}