2 Minutes To Code (β Version)

Easymock - 2 minute tutorial

How to define mock behaviour to throw excetion

Easymock let's us define mock's behaviour to throw exception when methods are called on it by using method andThrow(...) of class org.easymock.EasyMock.

Note that teh method replay(...) has to be called to mark the end of mock behaviour record state.

The example below shows hpw to do this.
package com.techfundaes.easymockBag;

import static org.easymock.EasyMock.*;

import java.util.List;

public class MockToThrowException
	public static void main(String[] args)
		List myList = createMock(List.class);
		expect(myList.get(0)).andThrow(new NullPointerException());