EasyMock Nice and Strict Mock

Filed Under: EasyMock

EasyMock mock objects can have three types – default, strict and nice. We can specify mock object type using MockType enum while creating the mock object.

EasyMock Mock Types

  1. Default: Expects only recorded calls but in any order. Test won’t fail if any recorded calls are skipped.
  2. Strict: Expects only recorded calls and in the same order they have been recorded.
  3. Nice: Expects recorded calls in any order and return default empty values (0, null, false) for unexpected calls.

We can create strict or nice mock object by specifying it’s mock type.


Object mock = mock(MockType.STRICT, Object.class);

Object mock1 = mock(MockType.NICE, Object.class);

If you are using @Mock annotation, then you can specify the mock type by any of following ways.


@Mock(MockType.STRICT)
Object obj;

@Mock(type=MockType.NICE)
Object obj1;

EasyMock Strict Mock Example

Let’s look at a simple example of the strict mock type. We will record few behaviors of an ArrayList mock object and replay them in the same order.


package com.journaldev.easymock;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;

import org.easymock.MockType;
import org.junit.jupiter.api.Test;

public class EasyMockStrictMockExample {

	@Test
	public void test() {
		ArrayList<Integer> mockList = mock(MockType.STRICT, ArrayList.class);
		expect(mockList.add(10)).andReturn(true);
		expect(mockList.add(20)).andReturn(true);
		expect(mockList.size()).andReturn(2);
		expect(mockList.get(0)).andReturn(10);
		replay(mockList);

		mockList.add(10);
		mockList.add(20);
		assertEquals(mockList.size(), 2);
		assertTrue(mockList.get(0) == 10);

		verify(mockList);
	}
}

If an unexpected method is called on a strict mock object, the error message will show the method calls expected at this point followed by the first conflicting method call.

If there are missing calls, then verify() method error message will show all missing method calls.

EasyMock Nice Mock Example

Now let’s have a look at the nice mock example. We will make some unrecorded calls and notice that the returned value is the default value of the method return type.


package com.journaldev.easymock;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.ArrayList;

import org.easymock.Mock;
import org.easymock.MockType;
import org.junit.jupiter.api.Test;

public class EasyMockNiceMockExample {
	
	@Test
	public void test() {
		ArrayList<Integer> mockList = mock(MockType.NICE, ArrayList.class);
		expect(mockList.add(10)).andReturn(true);
		expect(mockList.size()).andReturn(2);
		expect(mockList.get(0)).andReturn(10);
		replay(mockList);

		mockList.add(10);
		// below will NOT throw exception because of nice mock
		boolean b = mockList.add(30);
		assertFalse(b);

		assertEquals(mockList.size(), 2);

		assertTrue(mockList.get(0) == 10);
		//verify won't throw error for unexpected calls for nice mock
		verify(mockList);
	}
}

Summary

EasyMock strict mocking can be handy when our test methods have to be called in specific order. For example, CRUD operations mocking. Similarly, if we want to allow unexpected calls to pass, we can use nice mock.

You can checkout complete project and more EasyMock examples from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages