We just started using Mockito for unit testing of our Wicket applications. Its a great tool, allowing us to easily mock out our service layers on a test-by-test basis.

In the example below, we’re testing a page called ItemPage. It takes an id of an item to load – internally the page uses itemService.find(id) to load the item from the database. Using Mockito, we mock out the itemService.find call to return a test item.

public class TestItemPage extends WicketTestcase {
	private static final long serialVersionUID = 1L;

	public void TestItemPage() {
		when(itemService.find(Mockito.anyInt())).thenAnswer(new Answer() {

			public Item answer(InvocationOnMock invocation) throws Throwable {
				final int id = (Item) invocation.getArguments()[0];
				Item item = new Item(id);
				item.setDeadline(new Date());
				return item;

		tester.startPage(new ItemPage(12345));

Leave a Reply

Your email address will not be published.