EasyMock Verify

Filed Under: EasyMock

EasyMock Verify method is used to verify that all the stubbed methods are called and there are no unexpected calls on the mocked object.

EasyMock Verify

EasyMock verify() method has the same effect as calling verifyRecording(Object) and verifyUnexpectedCalls(Object) methods.

Let’s look at a simple example of EasyMock verify() method.


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.junit.jupiter.api.Test;

public class EasyMockVerifyExample {

	@Test
	public void test() {
		ArrayList<Integer> mockList = mock(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);
		assertTrue(mockList.get(0) == 10);
		assertEquals(mockList.size(), 2);

		verify(mockList);
	}
}

Notice that all the stubbed methods are being called after replay() method.

What if we don’t invoke a stubbed method?

It will throw AssertionError. For example, if I comment mockList.add(10) statement then below exception stack trace will be reported by JUnit.


java.lang.AssertionError: 
  Expectation failure on verify:
    ArrayList.add(10 (int)): expected: 1, actual: 0
	at org.easymock.internal.MocksControl.verify(MocksControl.java:284)
	at org.easymock.EasyMock.verify(EasyMock.java:2041)
	at com.journaldev.easymock.EasyMockVerifyExample.test(EasyMockVerifyExample.java:31)

EasyMock Verify example

EasyMock verify unexpected calls with Nice Mock

If the mocked object is a nice mock, then verify() method will not throw an error for unexpected method calls. However, it will throw an error if stubbed calls are not invoked.

Below code snippet for test will pass with green colors because mocked object is nice mock.


ArrayList mockList = mock(MockType.NICE, ArrayList.class);
replay(mockList);
mockList.add(10); //unexpected method call
verify(mockList);

EasyMock verify() Complex Example

We can move EasyMock verify() method to the @After methods to make sure all the stubbed methods were being called.


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.assertTrue;

import java.util.ArrayList;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class EasyMockVerifyRealExample {

	ArrayList<Integer> mockList;

	@BeforeEach
	public void setup() {
		mockList = mock(ArrayList.class);
	}

	@Test
	public void test() {
		expect(mockList.add(10)).andReturn(true);
		expect(mockList.get(0)).andReturn(10);
		replay(mockList);

		mockList.add(10);
		assertTrue(mockList.get(0) == 10);
	}

	@AfterEach
	public void teardown() {
		verify(mockList);
	}

}

Summary

EasyMock verify() method is used to make sure that all the stubbed methods are being utilized and there are no unexpected calls. The behavior for unexpected calls changes for nice mock objects where it doesn’t throw any error. EasyMock verify() method is very similar to Mockito verify() method.

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